473,503 Members | 13,381 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to maintain session when using CURL?

rizwan6feb
108 New Member
Hi everyone, I am trying to get contents of a web page which requires login, how can i access that page with CURL?
May 31 '08 #1
4 21210
rizwan6feb
108 New Member
No help :( ?
I am missing you experts? Especially Atli, seems as if you are enjoying ur weekend :-? ?
May 31 '08 #2
rizwan6feb
108 New Member
No help :( ?
I am missing you experts? Especially Atli, seems as if you are enjoying ur weekend :-? ?
May 31 '08 #3
rizwan6feb
108 New Member
I have found a solution myself, using the following class
http://github.com/shuber/curl/tree/master
May 31 '08 #4
Atli
5,058 Recognized Expert Expert
Hi.
Glad you found a solution.

Let me suggest another one tho.

You can have cUrl save all cookies by setting the CURLOPT_COOKIEFILE and CURLOPT_COOKIEJAR options. So if the site uses Sessions to log you in, you can simply pass the POST data required, and then save the Session ID cookie to maintain the session.

Say the server has a script like this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3.  
  4. if(isset($_SESSION['UserName'])) {
  5.     echo "You are logged in as ". $_SESSION['UserName'];
  6. }
  7. else if(isset($_POST['UserName'])) {
  8.     $_SESSION['UserName'] = $_POST['UserName'];
  9.     echo "You have been logged in as ". $_POST['UserName'];
  10. }
  11. else {
  12.     echo "No username available in session or post.";
  13. }
  14. ?>
  15.  
You can log in by doing something like this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Create cookie file if it doesn't exists
  3. $cookieFile = "myCookiefile.txt";
  4. if(!file_exists($cookieFile)) {
  5.     $fh = fopen($cookieFile, "w");
  6.     fwrite($fh, "");
  7.     fclose($fh);
  8. }
  9.  
  10. // Initialize the cUrl connection
  11. $ch = curl_init();
  12. curl_setopt($ch, CURLOPT_URL, 'http://localhost/login.php');
  13.  
  14. // Set the POST data
  15. $data = array('UserName' => 'Foo');
  16. curl_setopt($ch, CURLOPT_POST, 1);
  17. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  18.  
  19. // Set the COOKIE files
  20. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);
  21. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
  22.  
  23. // Send request and print results
  24. curl_exec($ch);
  25.  
  26. // Close the connection
  27. curl_close($ch);
  28. ?>
  29.  
It will log you in on the first execution but print the "You are logged in" message after that.
May 31 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1280
by: Seth Broomer | last post by:
I am losing my session state for some reason randomly and i don't know why i store just simple things like an id for the user, and their username, etc. maybe 10 items per user it happens about...
4
2236
by: Oscar Onorato | last post by:
Hello colleagues: Somebody know that good alternatives I have, in addition to the cookies, to maintain the session state? Where an example of it can be seen? Thanks
2
3736
by: David Berman | last post by:
It seems that my site is losing session information when using Server.Transfer. I have a page called PictureGallery.aspx. It takes an argument which is an index id, so it would look like...
1
7714
by: zwieback89 | last post by:
Hi, I have a org tree with hierarchical display of employees built using classic asp and vbscript. I also have list of radio buttons for report names. I have 1 select box with dates in it....
0
1612
by: gagoo | last post by:
Aslamoalikum I have problem that using php i create session in wap application it creates session. it send sessionid to all other url in protected pages but not maitain session.when i accessed...
13
3856
by: princei2007 | last post by:
hi, iam just a beginner with php and curl.i search a lot but not able to find out the find out the problem i am gating. I am first sending data to a php a file then using CURL on the php file iam...
3
10348
by: JDS | last post by:
So, I'd like to create the following scenario: 1) Use cURL library within PHP (cURL + "Cookie Jar", et.al) to create a virtual browser session that "logs in" to a remote site. (For example: ...
1
3181
by: vaibhavz | last post by:
hi i'm having one application in that i'm using one session variable. my code works fine till the user hits refresh button but after this session variable becomes empty. below is the code...
3
6277
by: buzz2050 | last post by:
Hi all, I am using cURL for the first time. I need to login to a site and my cURL code to do the same is as follows: //curlScript.php <?php function getContent($url, $referer,...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7364
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1524
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.