简体   繁体   中英

Php Curl Login Cookie How to set cookies

i have two sites A and B i would like to be able to have a verified user from site A log on to site B,

Site A - uses php;

Site B - uses Zend Framework.

i would like to have a iframe on site A to the private part of site B or a redirect to Site B when i do a html post from a form on site A with the credentials i get redirected and logged in everything works, i would like to do it more securely from the server side in php. when i post using php curl the user does not get logged in inside the iframe, i tired redirecting but again the user is not logged in on Site B.

I dont know why html form works and php curl doesn't? am i suppose to send something else using curl, or is a problem with Zend?

----- ok after some more googling i have narrowed the problem down to the cookies i believe i have to set them but i dont know how? is this correct? how would i go about setting them in php? or do i pass on the header where the cookie is set to the browser and forward to site B? if so how do i do this?

here is my code so far.

<?php
$data = array(
 'useremail' => 'someemail',
 'password' => 'somepassword'
 );

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://siteb.com/account/login");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE );
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");   // Cookie management.
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);

$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
?>    

<html>
 <body>
 <iframe src="http://SiteB.com/account/login" width="950" height="700"></iframe>

 <form action="http://SiteB.com/account/login" method="post">
 email: <input type="text" name="useremail" value="useremail" /><br />
 password: <input type="text" name="password" value="somepassword" /><br />
 <input type="submit" value="Submit" />
 </form>
</body>
</html>

thanks for your time and help.

" when i post using php curl the user does not get logged in inside the iframe, i tired redirecting but again the user is not logged in on Site B."

my ans : Think of Php-Curl as a broswer. And the broswer you are executing the script as a different browser. for the iframe authentication to work, the cookie needs to be set int he browser. When you authenticate from a PHP script , the cookie is set on the PHP-curl broswer. Both of these broswers do not share cookies. I dont think you can make the user login like this.

Are you using a Windows server or a Linux server?

i believe i have to set them but i dont know how? is this correct? You are doing it right by seeting the curl options to use cookies.

curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);   // Cookie management.
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);

Btw, Can you try the below code and check if the cookies%randNum%.txt is being generated in your cookies folder? Btw create a cookies folder in the same folder as the script before executing. Just to make sure you ahve all the cookies in a folder rather than spread amongst the script files.

    <?
$mypath = getcwd();
            $mypath = preg_replace('/\\\\/', '/', $mypath);
            $rand = rand(1, 15000);
            $cookie_file_path = "$mypath/cookies/cookie$rand.txt";
    $data = array(
     'useremail' => 'someemail',
     'password' => 'somepassword'
     );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://siteb.com/account/login");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE );
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);   // Cookie management.
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
    curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);

    $output = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);
?>

Did you create cookies.txt and allow the web server's user to write to it?

http://siteb.com/account/login will need to start a session (or write cookies).

cURL will handle the rest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM