Trouble with SESSION variables

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
dbudde
Forum Commoner
Posts: 25
Joined: Mon Dec 01, 2003 4:02 pm
Location: Chicago, IL

Trouble with SESSION variables

Post by dbudde »

OK, I am having trouble getting my page2 recognize that page1 created the variable $_SESSION['logged_in'] because when I go to page1 and do login and go to page2, it never allows me to reach page2, my code automatically redirects me back to the page1 (login page) because it does not see that $_SESSION['logged_in'] exists (isset).

Below is the code that I am concerned about because it is not working how I beleive it should work. Any help would be greatly appreciated. I have programmed in PHP before, but it has been around 3 yrs now.

PAGE1

Code: Select all

<?php 
     session_start(); 
     $_SESSION['logged_in'] = "False"; 
?> 

PAGE2 

<?php
     session_start();
     if (!isset($_SESSION['logged_in'])) { 
          header("Location: http://returntopage1"); 
          /* Redirect browser */ 

          /* Make sure code below does not execute when we redirect. */ 
          exit; 
     } 
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Please bump your original topic instead of starting a new one on the same question - it makes it a lot easier to follow a thread.

Ok, have you checked what's in the $_SESSION array on page 2? For debugging, change page2 to:

Code: Select all

session_start(); 
echo '<pre>';
print_r($_SESSION);
echo '</pre>';
Mac
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

In your php.ini, is your session.save_path set to a folder that actually exists?
dbudde
Forum Commoner
Posts: 25
Joined: Mon Dec 01, 2003 4:02 pm
Location: Chicago, IL

ok, sorry about starting a new topic

Post by dbudde »

I was just trying to clean up my noobish attempt at showing my code.

I beleive the session.save_path is set to /tmp on my linux server which does exist, I will double check it. I will also double check to make sure that php is able to write to that directory. If this is supposed to be a path on the clients pc? please let me know and I will have to fix that.

twigletmac, I will try your idea as soon as I get home and let you know what I find, thanks for your help thus far.
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

It is the path on your server, so you probably have it correct. Hopefully it's the permissions problem.
dbudde
Forum Commoner
Posts: 25
Joined: Mon Dec 01, 2003 4:02 pm
Location: Chicago, IL

ok, i checked that /tmp directory

Post by dbudde »

Ok, I checked my /tmp directory and found a bunch of files starting with sess_.... which looked to be the session files, so I noticed none had been created today, so I went into my site and tried to access the pages as usual, then I rechecked the tmp directory and found a sess_.... file with todays date on it, so it appears to be able to have access to the directory and write to it.
dbudde
Forum Commoner
Posts: 25
Joined: Mon Dec 01, 2003 4:02 pm
Location: Chicago, IL

I tried your idea twigletmac

Post by dbudde »

Ok, I tried your idea twigletmac and this is what was returned to me on the page2.

Array
(
)

Which basically tells me that the array is empty? correct?
So, where is my problem here? Do I need to submit a copy of my php.ini?
dbudde
Forum Commoner
Posts: 25
Joined: Mon Dec 01, 2003 4:02 pm
Location: Chicago, IL

I did something else

Post by dbudde »

I just wanted to let you know that I tried something else. On page1 after "logged_in" is created I added the code you gave me and there it shows up in the $_SESSION array, but when I go to page2 it does not show up. I don't know if that will help any as far as debugging purposes.
dbudde
Forum Commoner
Posts: 25
Joined: Mon Dec 01, 2003 4:02 pm
Location: Chicago, IL

Please, someone help me

Post by dbudde »

Please, someone help me
dbudde
Forum Commoner
Posts: 25
Joined: Mon Dec 01, 2003 4:02 pm
Location: Chicago, IL

I found my problem

Post by dbudde »

If figured out what was wrong, if someone wants to tell me why this made a difference I would greatly appreciate it, but all it took to get my code working was to add "SID" to the SESSION_START().

Code: Select all

session_start(SID);
All the coding I have seen never mentions adding this in, someone want to explain to me why this made the difference please? Other than that my problem is solved thus far.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Could you show us the session part of your php.ini so we can compare the settings to our own (might help us pinpoint the problem).

Mac
dbudde
Forum Commoner
Posts: 25
Joined: Mon Dec 01, 2003 4:02 pm
Location: Chicago, IL

ya, I will post my php.ini this evening

Post by dbudde »

Ya, I will post my PHP.INI later this evening, just the Session section as you requested, thanks for the reply.
dbudde
Forum Commoner
Posts: 25
Joined: Mon Dec 01, 2003 4:02 pm
Location: Chicago, IL

here is my session section of php.ini

Post by dbudde »

Ok, here is my php.ini SESSION section. Any of the lines with a semi-colon in front of it are included in the file, but are obviously not being used. Tell me what you think.

Code: Select all

&#1111;Session]

session.save_handler = files

session.save_path = /tmp

session.use_cookies = 1

session.use_only_cookies = 1

session.name = SID

session.auto_start = 0

session.cookie_lifetime = 0

session.cookie_path = /

session.cookie_domain =

session.serialize_handler = php

session.gc_probability = 1
session.gc_dividend    = 100

session.gc_maxlifetime = 1440

session.bug_compat_42 = 1
session.bug_compat_warn = 1

session.referer_check =

session.entropy_length = 0

session.entropy_file =

;session.entropy_length = 16

;session.entropy_file = /dev/urandom

session.cache_limiter = nocache

session.cache_expire = 180

session.use_trans_sid = 0

url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=,fieldset="
Post Reply