Page 1 of 1
Problem retrieving session variables
Posted: Tue Oct 12, 2004 7:03 am
by mlkarie
twigletmac | Help us, help you. Please use Code: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
I have written a complete login system in PHP/MySQL, which works fine on my local webserver. When I test this on the remote server where the site is to be hosted, cookies don’t work (disabled on that server?). So I’ve tried passing the session id via the URL. This works, but the session variables (name and surname) are not retrieved. So my questions are:
What is the correct way to pass the session id via a URL?
What is the correct way to access and display the user’s name and password in subsequent pages?
Code from the login page
Code: Select all
$go_ahead = true; // set this to true for now – will perform proper function later
if ($go_ahead) { //register session variables
$_SESSION['session_id'] = session_id();
$_SESSION['name'] = $name;
$_SESSION['surname'] = $surname;
--- The code above creates a session file on the server; I’ve opened the session file with Notepad and it contains the id, and the correct name and surname.
Another page is called up with an URL similar to this:
page2.php?session_id=$session_id
Code from page2.php:
Code: Select all
session_start();
$session_id = $_GET['session_id'];
It correctly displays the session id when I say <?php echo $session_id; ?>
Code: Select all
if (isset($session_id)) {
$_SESSION['session_id'] = $session_id;
$name = $_SESSION['name'];
$surname = $_SESSION['surname'];
However, the name and surname are not set.
What is the correct way to link the session id (passed through the URL) to the user’s current session, with corresponding session variables?
Please help, thx
Posted: Tue Oct 12, 2004 7:15 am
by twigletmac
Could you check the output of [php_man]phpinfo()[/php_man]:
and tell us the values of session.use_cookies and session.use_trans_sid?
Mac
Posted: Tue Oct 12, 2004 12:22 pm
by mlkarie
The values of session.use_cookies and session.use_trans_sid are ON for both local values and master values
To Twigletmac:
Apologies for not posting the code properly. I'm new to this forum, will check out the guidelines and do it properly next time.
Posted: Wed Oct 13, 2004 3:43 am
by twigletmac
Then you shouldn't be having the issues you have because of the server settings, so firstly make sure that you have session_start() on all pages in which you wish to use sessions, if that's fine make sure that your error reporting is set to the highest level and that errors are set to display by adding the following code to the start of the script:
Code: Select all
ini_set('display_errors', 1);
error_reporting(E_ALL);
and see if you get any notices or warnings.
Mac
Problem retrieving session variables
Posted: Wed Oct 13, 2004 6:50 am
by mlkarie
I've turned on the error reporting and get the following:
On the first call to this page (when the user has not logged on), the error message states "undefined index (session_id) on line 11", which makes sense because at that stage the session_id has not been created.
After logging on and refreshing to the same page, there are two new "undefined index" messages, first one for "name", second one for "surname". No such message for the session_id as this was passed from the URL and received by the $_GET statement.
So the error messages are confirming that name and surname can't be found.
However, the session file that I downloaded shows that the name and surname were created.
(I do have session_start() at the top of the page.)
Posted: Wed Oct 13, 2004 7:12 am
by kettle_drum
Try:
To see if your managing to set any session variables and then work your way backwards to see where your problem is.
Problem retrieving session variables
Posted: Wed Oct 13, 2004 7:38 am
by mlkarie
feyd | Help us, help you. Please use Code: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Thanks, this is what I get from the print statement:
Code: Select all
Array ( їsession_id] => c35a2559c95ede3bb5255f6f255a4d5f )
Below is the contents of the session file which I downloaded off the server:
Code: Select all
session_id|s:32:"c35a2559c95ede3bb5255f6f255a4d5f";name|s:5:"este5";surname|s:7:"shearar";
It appears to me that my code on line 2 below:
Code: Select all
<?php
if (isset($session_id)) {
$_SESSION['session_id'] = $session_id;
$name = $_SESSION['name'];
$surname = $_SESSION['surname'];
print_r($_SESSION);
?>
creates a new array with the session id c35a.... as its only element, instead of picking up the existing session file, which has the same session id, and the two elements "name" and "surname"
So what I need to know is:
1. why doesn't session_start() pick up the existing session array?
2. Should my line 2 above not be there?
To recap previous stuff:
When I tried this with cookies (not passing the session id in the URL), it worked absolutely fine on my local computer's web server. But nothing works on the remote server.
feyd | Help us, help you. Please use Code: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Posted: Wed Oct 13, 2004 8:10 am
by kettle_drum
What happens with the following:
Code: Select all
<?php
session_start();
print_r($_SESSION);
session_end();
?>
If this doesnt show all the vars that you hope to have set, then they havent been set correctly.
Problem retrieving session variables
Posted: Wed Oct 13, 2004 8:28 am
by mlkarie
I put in:
Code: Select all
<?php
session_start();
print_r($_SESSION);
// without the session_end(), which Dreamweaver/PHP doesn't recognise?
When I go to the page before log on, it gives:
Array ( [session_id] => )
After logging on it gives:
Array ( [session_id] => c35a2559c95ede3bb5255f6f255a4d5f )
i.e. no name, surname, and it seems to be the same session id as before.
But if the session variables "name" and "surname" were not set correctly, how is it possible that the correct values for the variables actually show up in the session file when I open it with Notepad?
?>
Posted: Thu Oct 14, 2004 3:46 am
by twigletmac
For some reason the session isn't being passed between each page (is the cookie not being set - this would be a browser rather than server issue), instead of setting the session ID as you currently are, which can't work as PHP can't relate what you're passing in the URL to a session, try the example from the [php_man]session_start()[/php_man] documentation:
Code: Select all
// Or maybe pass along the session id, if needed
echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
Mac
Problem retrieving session variables
Posted: Wed Oct 20, 2004 5:56 am
by mlkarie
I have discovered that the reason why the session id is not being passed is related to this being a PHP/Flash system.
When I use the loadvars.sendandload command in Flash, data is sent back and forth between the Flash movie and PHP, without reloading the HTML file, and the session id is not received by the browser (I think).
I have now altered the system to use a loadvars.send, which sends the data to a PHP file, which reloads the original HTML file with altered variables. This works.
I still need to find out if it’s possible to use the sendandload command, and have the session id passed to the browser, as the aim of this system is to log a person on and display the name and surname, without reloading the HTML, then pass on session id’s once the user clicks on other links.
Will try SID, and also post a topic about this on some Flash forums.
Thanks for the help.
Posted: Wed Oct 20, 2004 8:44 am
by feyd
send the session id as apart of the original page.
