Page 1 of 1

Can't set session, Help!!!

Posted: Fri Mar 07, 2003 1:20 pm
by mikebr
I have built a MySQL db on my laptop which seems to run without any problems but on loading it to myn server I find that the session is not getting set. IE5 and cookies are set for "Accept all" but the session does not get listed in the cookie list in the browser settings so the user gets redirected to my login page again as he should if the variables are empty on being checked. My server is using the same PHP version I have on my laptop "PHP4.2.3". Has anyone any idea what thyis problem can be? I have tried looking this up on the PHP manual but I seem to be working under it's guidlines.
The very simple code I am using to set the session is:

Code: Select all

<?php
$c_password = $_POST['c_password'];
$c_login = $_POST['c_login'];
$browse_time = +3600;
$ufs = "mix";

session_start();

header ("Cache-control: private");

$salt = substr($ufs, 0, 3);
$crypted_password = crypt($c_password, $salt);

include "inc_defaults.php"; // Default variables

session_register('crypted_password');
$_SESSION['crypted_password'] = $crypted_password;

session_register('c_login');
$_SESSION['c_login'] = $c_login;

session_register('expire');
$_SESSION['expire'] = time() + $browse_time; // Browse time is a default variable

header("Location: ../membersarea.php");
exit;
?>

Posted: Fri Mar 07, 2003 1:25 pm
by daven
put session_start() at the top of your page.

When using superglobals ($_SESSION[]), you do not need to use session_register()

Posted: Fri Mar 07, 2003 1:53 pm
by mikebr
Thanks for the help, but changing the code to the following still doesn't set the session?

Code: Select all

<?php
session_start(); 
$c_password = $_POST['c_password']; 
$c_login = $_POST['c_login']; 
$browse_time = +3600; 
$ufs = "mix"; 

header ("Cache-control: private"); 

$salt = substr($ufs, 0, 3); 
$crypted_password = crypt($c_password, $salt); 

$_SESSION['crypted_password'] = $crypted_password; 
$_SESSION['c_login'] = $c_login; 
$_SESSION['expire'] = time() + $browse_time; // Browse time is a default variable 

header("Location: ../membersarea.php"); 
exit; 
?>

Posted: Fri Mar 07, 2003 11:34 pm
by protokol
I'm assuming that what you mean is that when you redirect to 'membersarea.php' you can't get the $_SESSION variables right?

Make sure that you have session_start() at the top of every page that needs to use the session information

Posted: Sat Mar 08, 2003 12:34 am
by phice
Exactly what protokol said.

Use session_start(); on the very first line (after the <?, of course) of your php document that you wish to use session variables.

echo $_SESSION["variable_name"]; when you choose to print out the value of the session variable.

Posted: Sat Mar 08, 2003 2:49 am
by mikebr
Sorry guys, I checked on another 2 computers and the session is getting set on them, the problem seems to be on the computer I have been working on. Weird, the session does not get set in any browser on my desktop machine but on my PC and laptop it does??????

Thanks for the help, I guess I need to trash my internet prefeences on my computer and try again.

Posted: Sat Mar 08, 2003 2:12 pm
by phice
Tools > Internet Options > [TAB] Advanced > [BUTTON] Restore Defaults

Posted: Sat Mar 08, 2003 6:45 pm
by evilcoder
ermmm sessions are serverside matey. Your browsers cant reject them. :)

Posted: Sat Mar 08, 2003 7:01 pm
by phice
Well it wouldn't hurt to reset his IE settings. ;)

Posted: Sat Mar 08, 2003 7:16 pm
by evilcoder
lol true... i was just telling it how it is.

Posted: Sat Mar 08, 2003 7:17 pm
by hob_goblin
evilcoder wrote:ermmm sessions are serverside matey. Your browsers cant reject them. :)
Not really, the browser needs to set a cookie to determine the session ID.

If the cookie isn't set (and the SID is not appended to the URL), the server won't know what session to use.

So, check if the browser has cookies enabled, (which the IE reset might fix).

Posted: Sat Mar 08, 2003 7:27 pm
by evilcoder
i have cookies disabled on my PC, and session always work perfectly.

Posted: Sat Mar 08, 2003 7:30 pm
by hob_goblin
session_start() creates a session or resumes the current one based on the current session id that's being passed via a request, such as GET, POST, or a cookie.
the browser has to tell the server the id.

Posted: Sat Mar 08, 2003 11:02 pm
by protokol
evilcoder wrote:i have cookies disabled on my PC, and session always work perfectly.
So the way that you would be able to have sessions work then is if the session_id() is passed to the URL or through hidden form inputs.

Posted: Sat Mar 08, 2003 11:17 pm
by evilcoder
dunno, but i go to alot of sites using sessions and i never have a probably and the session id is never parsed through the url.