PHP session problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
michal10802
Forum Newbie
Posts: 4
Joined: Wed Jun 25, 2014 8:14 am

PHP session problem

Post by michal10802 »

Hi, i have folowing problem.
I have one main page that contains iframe page. I need to send post request from main page to login page of iframe page. All works great if post request from main page is posting via web form. Then on iframe page user are authentificated and stored in session variable and iframe page works and showing pages for only authorized user.
Problem is when i trying different way of posting request to frame login page, for example via jquery or via curl from php. Then session variable not storred and iframe page not working and not showing pages for authorized user.
Can anyone help?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP session problem

Post by Christopher »

The session variable is stored, but some of these sessions may be within the same request. If so use session_write_close() to write the session data back. Then you need to make sure that you are re-reading the session data.
(#10850)
michal10802
Forum Newbie
Posts: 4
Joined: Wed Jun 25, 2014 8:14 am

Re: PHP session problem

Post by michal10802 »

thx for reply.
But i am not sure, if i understand exactly what you mean.
I tried use session_write_close() after setting session but nothing happens.
Could you be please more specified what you meant with "make sure that you are re-reading the session data."
Thansk a lot
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP session problem

Post by Christopher »

michal10802 wrote:Could you be please more specified what you meant with "make sure that you are re-reading the session data."
If you just go HTTP request to HTTP request then PHP sessions work easily. But it sounds like you are using Ajax or cURL to do some of the requests. If you do that you need to make sure that those request are using the same session ID as the page that is calling them (especially for cURL). And understand that if you update session data in an Ajax call (for example) that the session data on the current page will not be updated. You need to make another request to get the updated session data. Remember that session data is read at the beginning of a HTTP request and written at the end. Also be aware that if you are doing async calls that the one writing the session data may end after the one reading the session data.
(#10850)
michal10802
Forum Newbie
Posts: 4
Joined: Wed Jun 25, 2014 8:14 am

Re: PHP session problem

Post by michal10802 »

and how to set session id to curl request? i try it with adding PHPSESSID with value to the requesting url and adding to the curl option
CURLOPT_COOKIE => session_name().'='.session_id(), and both didnt work, still session not stored.
Can anyone different idea where i am do wrong? or it is even possible to do something that?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP session problem

Post by Christopher »

Is cURL calling a script on the same domain?

In the manual I found this example that shows getting the session ID and writing the session data before calling cURL:

Code: Select all

<?php
session_start();
$strCookie = 'PHPSESSID=' . $_COOKIE['PHPSESSID'] . '; path=/';
session_write_close();

$curl_handle = curl_init('enter_external_url_here');
curl_setopt( $curl_handle, CURLOPT_COOKIE, $strCookie );
curl_exec($curl_handle);
curl_close($curl_handle);
?> 
(#10850)
michal10802
Forum Newbie
Posts: 4
Joined: Wed Jun 25, 2014 8:14 am

Re: PHP session problem

Post by michal10802 »

i found this manual too but not worked, but i found too that curl after call curl_close clean any session, which was stored on server during curl request.
So curl is not right option for my problem.
Then i tried different way how send request to my iframe page. I try Header("Location: ".$url) and then on iframe page after processed request redirect back. All works, all session are stored but i must send all data in url as get parameter and it is not safe because it contains password too.
So it is any way to send or any way to do redirection with additional post data to another domain except curl?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP session problem

Post by Christopher »

You should be able to get cURL to work. If you are doing redirection then you need to call session_write_close() before redirecting because both pages are in the same HTTP request.

You have not posted any code, so it is not possible to tell what you are doing wrong. If you can code a small test script that demonstrates the problem then perhaps someone can help.
(#10850)
Post Reply