Menu DaniWeb
Log In Sign Up
  • Read
  • Contribute
  • Meet
  1. Home
  2. Programming Forum
  3. Web Development Forum
  4. Code Snippet Repository
  5. Reusable Code Snippet

Curl login authentication cookie issues

15 Years Ago beanonymous 0 Tallied Votes 186 Views Share

OK here is a piece of code I've been working on that actually logs you in and returns what ever Curl sees except i have no idea how to make Curl function correctly with websites that require cookie authentication correctly, can anyone help? =/

php
<?php

//create array of data to be posted
  $post_data['username_here'] = "username";
  $post_data['password_here'] = "password";
  
  //traverse array and prepare data for posting (key1=value1)
  foreach ( $post_data as $key => $value) {
   $post_items[] = $key . '=' . $value;
  }
 
  //create the final string to be posted using implode()
  $post_string = implode ('&', $post_items);
  $post_string = "dest=%2".$post_string ."&submit=next";
  //i.e. ?dest=%2F&username=username_here&password=password_here&submit=next
  

  //create cURL connection  
  $curl_connection = curl_init("https://my.netspace.net.au/logincheck");
  
  //set options
  curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30); //set time untill cURL times out
  curl_setopt($curl_connection, CURLOPT_USERAGENT, 
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"); // set HTTP User Agent
  curl_setopt($curl_connection, CURLOPT_HEADER,1);
  curl_setopt($curl_connection, CURLOPT_POST, 1);
  curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);//set data to be posted
  curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1); // follow redirection if any...
  curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false); //determin if to continue processing code if SSL certificate is verifyed or not...
  
  //cookie code ive found that dont seem to work...
  /*curl_setopt($curl_connection, CURLOPT_COOKIEFILE, "cookies.txt");
  curl_setopt($ch, CURLOPT_COOKIE, "cookies.txt");
  curl_setopt($curl_connection, CURLOPT_COOKIEJAR, "cookies.txt");
  curl_setopt($curl_connection, CURLOPT_COOKIESESSION, true); //return cookies that cURL collects*/
     
  //perform our request (execute request)
  $result = curl_exec($curl_connection);
  curl_close();
  echo $result;
?>
About the Author
Member Avatar for beanonymous
beanonymous 0 Newbie Poster
Be the first to reply
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.

Sign Up — It's Free!
Related Topics
  • Member Avatar cURL doesn't return the page? 5
  • Member Avatar php url and pagination 4
  • Member Avatar write variable to div dynamically 2
  • Member Avatar Cookie expire times 16
  • Member Avatar ECHO results to textfield 4
  • Member Avatar login at youtube using cURL and php ? 3
  • Member Avatar Sql Server Database Connector 1
  • Member Avatar PHP cookie 5
  • Member Avatar getting user info 12
  • Member Avatar PHP HTTP Screen-Scraping Class with Caching 1
  • Member Avatar Publishing php/mysql on Apache website 3
  • Member Avatar PHP Curl error: Location redirect while open_basedir is set 3
  • Member Avatar Bad XML 1
  • Member Avatar Php with curl using high cpu 4
  • Member Avatar Login Script Integration with PHP/MySql Wordcount/Story Script. 4
  • Member Avatar curl not working for getting a web page content, why? 4
  • Member Avatar Printing the contents of a div, keeping styling 6
  • Member Avatar Cookie is created but cannot be read 8
  • Member Avatar php-refresh iframe when parent page refreshes?? 1
  • Member Avatar php session to allow user to stay signed in forever 5
Not what you need?

Reach out to all the awesome people in our web development community by starting your own topic. We equally welcome both specific questions as well as open-ended discussions.

Start New Topic
Topics Feed
Reply to this Topic
Edit Preview

Share Post

Insert Code Block

  • Forums
  • Forum Index
  • Hardware/Software
    • Recommended Topics
  • Programming
    • Recommended Topics
  • Digital Media
    • Recommended Topics
  • Community Center
    • Recommended Topics
  • Latest Content
  • Newest Topics
  • Latest Topics
  • Latest Posts
  • Latest Comments
  • Top Tags
  • Topics Feed
  • Social
  • Top Members
  • Meet People
  • Community Functions
  • DaniWeb Premium
  • Newsletter Archive
  • Markdown Syntax
  • Community Rules
  • Developer APIs
  • Connect API
  • Forum API Docs
  • Tools
  • SEO Backlink Checker
  • Legal
  • Terms of Service
  • Privacy Policy
  • FAQ
  • About Us
  • Advertise
  • Contact Us
© 2025 DaniWeb® LLC