Page 1 of 2

Session variables not registering - works in IE not in FF

Posted: Wed Jun 04, 2008 4:46 pm
by sixdollarshirt
I know that the title doesn't make sense since Sessions are server side, but it's the truth.

At the top of my header I start a session so every page on the site can use the session vars

Code: Select all

<? session_start();?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>  etc...
I have a header which calls a login form function.

Code: Select all

function loginForm(){
 
//-- if user session isn't set show the form
if(!(isset($_SESSION['share']))){
        ?>
    <form method="POST" action="../authenticate.php">
                                 <p>username:<br/><input name="un" class="textbox" type="text" />
                                    password:<input name="pw" class="textbox" type="password" /><br/><br/>
                                        <input type="submit" class="searchbutton" value="login"><br/>
                                 </p>
                                 <a href="#">new users register here</a>
                     </form>
    <?
    }
else{
         echo "<p>Logged in as <span class=\"red\">".$_SESSION['un']."</span>.<br/><br/>";
         echo "<a href=\"http://www.bigfatstupid.com/_dev/share/admin/logout.php\">Logout</a></p>";
        }
    
}
That info is passed to my authentication page

Code: Select all

<?
//-- shareAuth.php
include("../includes/functions.php");
 
//-- grab data
$un = $_POST['un'];
$pw = $_POST['pw'];
 
//-- grab referer
$ref = getenv("HTTP_REFERER");
 
 
//-- connect to dbase
dbConnect();
 
//-- check auth data
$sql = "SELECT password FROM users WHERE '$un' = username";
$data = mysql_query($sql);
 
 
//-- check for username. if not found say so.
if(mysql_num_rows($data) == 0)
    {include("../includes/header.php");
         echo "<p>Username not found.<br><br>Please <a href=\"javascript&#058;history.go(-1)\">enter a correct username</a> 
                     or <a href=\"shareReg.php\">register to share</a>.</p>";
         include("../includes/footer.php");          
         exit();             
    }//-- end username check
 
                                                            
//-- check password
$data = mysql_fetch_row($data);
if($data[0]!= $pw)
    {include("../includes/header.php");
         echo "<p>The password is incorrect.<br><br>Please <a href=\"javascript&#058;history.go(-1)\">enter 
                  the correct password</a>.</p>";
         include("../includes/footer.php");          
         exit();
         }//-- end password check
 
 
//-- if password is correct show un in login window
else{
         session_start();
         $_SESSION['share'] = "granted";         
         $_SESSION['un'] = $un;
         header("Location: $ref"); 
} 
 
?>
 
If the user is authenticated the form is no longer shown and the username (stored in $_SESSION['un']) is shown. The problem is that, while this works in IE the session variable, $_SESSION['shared'], is never set when I try it in Firefox. It doesn't make any sense to me. :banghead:

ANY help would be greatly appreciated. I've looked everywhere for an answer and can't find one. Thank you so much for taking the time to read this post.

Re: Session variables not registering - works in IE not in FF

Posted: Wed Jun 04, 2008 8:53 pm
by deeessay
I can't/don't see anything wrong with your script.... why don't you try placing the session_start(); at the very first line of every php file.

Re: Session variables not registering - works in IE not in FF

Posted: Thu Jun 05, 2008 9:05 am
by sixdollarshirt
I placed the session_start() in my authentication script at the top of the page and am still having the same problem. This is the most ridiculous issue I have ever seen. Why would a browser NOT work with a server side session? The session variables are never set in Firefox for some reason.

If I change the code in the authentication script from this:

Code: Select all

$_SESSION['share'] = "granted";         
         $_SESSION['un'] = $un;
         header("Location: $ref");
to this:

Code: Select all

$_SESSION['share'] = "granted";         
         $_SESSION['un'] = $un;
         header("Location: http://www.someurl.com");
The session variables are set in Firefox, but only exist for the page you log in from. I'm getting to the point where I just want to throw all of the code out the window and start over. Very disappointing.

Re: Session variables not registering - works in IE not in FF

Posted: Thu Jun 05, 2008 9:17 am
by nowaydown1
You mentioned that your variables get set if you manipulate the $ref you're passing to your header() call by hand. Could you give us an example of what those URL's would actually look like if your sign in was working properly?

Re: Session variables not registering - works in IE not in FF

Posted: Thu Jun 05, 2008 9:25 am
by sixdollarshirt
thanks for responding.

basically the $ref var is set so that when people "log out" they're not brought back to the home page if they were 10 pages deep into a paginated search.

The URLs all look something like this.

Code: Select all

http://www.mysite.com/contact/contact.php
http://www.mysite.com/upload/upload.php
http://www.mysite.com/links/links.php
 
etc...

Re: Session variables not registering - works in IE not in FF

Posted: Thu Jun 05, 2008 9:30 am
by nowaydown1
Have you tried removing the full domain from your header redirect so it uses relative paths instead? (So /contact/contact.php instead http://www.mysite.com/contact/contact.php). I'm pretty sure $_SERVER["REQUEST_URI"] will give you the relative path info. You might wanna double check that though, it's been a while.

Re: Session variables not registering - works in IE not in FF

Posted: Thu Jun 05, 2008 9:42 am
by sixdollarshirt
Thanks for the help. I'll take a look at that. Though if that were the issue wouldn't it affect IE the same way since sessions are server side? VERY confusing.

Thanks again for your time. I do appreciate it when people take time out of their day to help others. Thank you.

Re: Session variables not registering - works in IE not in FF

Posted: Fri Jun 06, 2008 11:34 am
by sixdollarshirt
same stinking problem. I'm not sure what's going on here at all. I think I'm going to start over. There has to be a way to do what I want to do. If I ever figure this out I will surely let you know. :crazy:

Re: Session variables not registering - works in IE not in FF

Posted: Fri Jun 06, 2008 11:58 am
by dbemowsk
when you have session problems always look at your cookie settings. Especially if it is working in one browser and not the other. Your settings may be different between the two.

Re: Session variables not registering - works in IE not in FF

Posted: Tue Jun 10, 2008 2:18 pm
by sixdollarshirt
I was under the impression I'm not using cookies since Sessions are server side. Am I completely misunderstanding what I'm trying to do here?

Re: Session variables not registering - works in IE not in FF

Posted: Tue Jun 10, 2008 2:38 pm
by superdezign
sixdollarshirt wrote:I was under the impression I'm not using cookies since Sessions are server side.
Sessions are server-side. The session ID is stored client-side so that the server knows which session they belong to.

Re: Session variables not registering - works in IE not in FF

Posted: Tue Jun 10, 2008 4:53 pm
by dbemowsk
superdezign wrote: Sessions are server-side. The session ID is stored client-side so that the server knows which session they belong to.
Correct. The cookie is just used to pass the sess_id between pages. You should be able to use the sess_id in the URL in the event that you have cookies turned off, but I think that is a less secure way of doing it.

Re: Session variables not registering - works in IE not in FF

Posted: Tue Jun 10, 2008 5:00 pm
by superdezign
dbemowsk wrote:Correct. The cookie is just used to pass the sess_id between pages. You should be able to use the sess_id in the URL in the event that you have cookies turned off, but I think that is a less secure way of doing it.
More commonly, PHPSESSID. You can also use the build in constant 'SID' which contains 'name=ID' which is, by default, 'PHPSESSID=sessionId', where 'sessionId' is the actual session ID in order to easily put it into the query string of URLs.

Re: Session variables not registering - works in IE not in FF

Posted: Wed Jun 11, 2008 12:24 pm
by sixdollarshirt
Superdezign.
Do I need to create a cookie for this to work? I thought I would use either sessions OR cookies, but not both. I think I need to read up some more on how this is supposed to work. I thought I had a pretty good handle on how this was supposed to work, but I guess not.

Re: Session variables not registering - works in IE not in FF

Posted: Wed Jun 11, 2008 5:10 pm
by dbemowsk
No, you just need to have "session.use_cookies = 1" in your php.ini file. I think most web hosts have it on by default.