Cannot grab loginid need some help

I have problem in grabbing the value sesssion loginid if I use the curl to send data to my test.php
If I directly input the querysting in the url,I can see the echo loginid which is the value of 1 depending on the users id.

session_start();

$theloginid = 'No Loginid';
if(isset($_SESSION['loginid'])){
    $theloginid = $_SESSION['loginid'];
    echo $theloginid;
}else{
   echo $theloginid;
}

if I run this curl,I cannot get the value of loginid the session is not set,I don’t know why is not set.

$curl = curl_init();


curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'http://localhost/mysite/test.php',
    CURLOPT_HEADER => false

));


$resp = curl_exec($curl);

curl_close($curl);

echo $resp;

Thank you in advance.

According to the PHP manual:

Initializes a new session and return a cURL handle for use with the curl_setopt(), curl_exec(), and curl_close() functions.

So you’re going to need to pass to cURL the loginid from the normal PHP session and have cURL add it to the session data for the session that it has created.

I apologize, I am confuse on how to implement that.I could not get the idea.

Do I need to store the session in a text file during login (Authenticating the user)?

Found this via a google search

http://stackoverflow.com/questions/13020404/keeping-session-alive-with-curl-and-php

//initial request with login data

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/login.php');
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=XXXXX&password=XXXXX");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie-name');  //could be empty, but cause problems on some hosts
curl_setopt($ch, CURLOPT_COOKIEFILE, '/var/www/ip4.x/file/tmp');  //could be empty, but cause problems on some hosts
$answer = curl_exec($ch);
if (curl_error($ch)) {
    echo curl_error($ch);
}

//another request preserving the session

curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/profile');
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
$answer = curl_exec($ch);
if (curl_error($ch)) {
    echo curl_error($ch);
}

Looks like the solution is going to involve setting a cookie, sorry I can’t help you any further with this, I’ve never used cURL. @Drummin or @Hall_of_Famer have either of you used cURL before?

I have not. Sorry guys.

It’s been a few years, but yes, you sometimes need a Cookie for Curl because it isn’t a browser…

For simple DOM fetching etc., no. But if it requires logging in, yes.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.