Can't set session, Help!!!

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
mikebr
Forum Contributor
Posts: 243
Joined: Sat Sep 28, 2002 7:05 am

Can't set session, Help!!!

Post 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;
?>
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post by daven »

put session_start() at the top of your page.

When using superglobals ($_SESSION[]), you do not need to use session_register()
mikebr
Forum Contributor
Posts: 243
Joined: Sat Sep 28, 2002 7:05 am

Post 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; 
?>
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post 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
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post 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.
Image Image
mikebr
Forum Contributor
Posts: 243
Joined: Sat Sep 28, 2002 7:05 am

Post 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.
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Tools > Internet Options > [TAB] Advanced > [BUTTON] Restore Defaults
Image Image
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

ermmm sessions are serverside matey. Your browsers cant reject them. :)
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Well it wouldn't hurt to reset his IE settings. ;)
Image Image
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

lol true... i was just telling it how it is.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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).
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

i have cookies disabled on my PC, and session always work perfectly.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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.
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post 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.
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post 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.
Post Reply