Jump to content

Session Variables Not Passing


maudise

Recommended Posts

Hi all,

 

I'm at a bit of a loss I'm afraid, I'm pretty new to PHP and have been looking to code a simple log in page connecting to a mysql database. I'm running firefox 16.0.2 and have my websites hosted with hostgator.com.

 

I've created the log in forms without too much trouble but what I can't seem to get is the $_Session variables to do keep the variables between pages. In an attempt to isolate the problem I have stripped the issue right back to just passing the variables between two pages; sessionstart.php and sessioncheck.php

 

the code in sessionstart.php is simple enough;

At the very top of my page I have the following code

<?php
session_start();
$_Session['test'] = 'this is a Session variable';
?>

and then as a test I put in two sections of code in the body of the HTML tags


<?php
$Teststring = "this is a test string";

print $Teststring;

?>
<?php


print $_Session['test'];
?>

I know the separate php tags aren't required but I was trying to play around with things. Both of those prints work fine and not a problem.

 

On sessioncheck.php I have the following code at the very top of my page

<?php
session_start();
echo $_Session['$test'];
echo 'I tried';

?>

 

sessioncheck.php doesn't load the session variables at all, also the switching between print and echo was simply done to ensure that that wasn't the problem at all.

 

I've done a fair amount of searching around and to pre-empt a few questions which might be queried I have the following answers;

  • Session_ID(); returns the same Session ID
  • the phpinfo file tells me the php.ini files are loaded successfully
  • I can't remember the third but the other option didn't work either.

I've got to the end of my very limited knowledge so if anyone could give me any advice I would greatly appreciate it!

 

Regards

Maudise

Link to comment
Share on other sites

A slight correction on the wording, to avoid any confusion: The name of the $_SESSION superglobal is case-sensitive, as with all variable names in PHP, and thus needs to be in upper-case. As that's how PHP has defined it.

The indices are also case-sensitive, but they can be set to either lower, upper or a mix of cases as you see fit.

Edited by Christian F.
Link to comment
Share on other sites

Hi,

 

I had noticed the use of the $ and I have now changed to $_SESSION interestingly it has changed how it is displayed in the design view of dream weaver from a box which says PHP to {session.test}.

 

However this hasn't solved my problem it still wont pass variables between the two pages.

 

Thanks for the help so far

Link to comment
Share on other sites

Hi,

 

I'm currently out of the office so cannot upload the full file but I'll do it in a few hours when I'm back. What I'm doing is writing the code and uploading to my webpage to test whether it works. There are probably more efficient ways of doing it out there.

 

So it's not actually DreamWeaver which is doing the parsing, but it's the webhost (hostgator.com)

 

Maudise

Link to comment
Share on other sites

Hi,

 

The sessionstart.php page is below, and the session check in the code box below that.

 

<?php
   session_start();
    $_SESSION['test'] = 'this is a Session variable';
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
I'm confused

<?php
$Teststring = "this is a test string";

print $Teststring;
   echo $_SESSION['test'];

?>
</br>
</p>
Why is this not working!


<?php


print $_SESSION['test'];
?>
</body>
</html>

 

And now for the Sessioncheck page

 

<?php
session_start();
echo $_SESSION['test'];
echo 'I tried';

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>


<body>
<?php
echo $_SESSION['test'];

?>

Proof updated again
</body>
</html>

 

Thanks

Maudise

Link to comment
Share on other sites

Do you have a link to the page, so that we can see this in effect ourselves?

 

The only things I can think of right now, is that the cookie isn't being set properly. Or that you might be doing things out of order, though unlikely. In any case, do make sure that you're not blocking the cookie, and check to see if it is indeed being sent with the request from the browser.

Link to comment
Share on other sites

Do you have php's error_reporting set to E_ALL and display_errors set to ON so that any session related errors will be reported and displayed?

 

You could have a session.save_path problem or a header error due to your file's character encoding containing the BOM (Byte Order Mark) characters.

Link to comment
Share on other sites

Hi,

 

I put the following code at the start, on line 2 for both pages.

ini_set('display_errors',1);
error_reporting(E_ALL);

 

I now get Notice: Undefined index: test in /home/maudise/public_html/testing/sessioncheck.php on line 5 as an error.

 

For what it's worth, if you need to look at the phpinfo file it can be found at www.crosstrendanalysis.co.uk/testing/phpinfo.php.

 

Thanks for the continued help, I did look around for the undefined index problem but couldnt' find a solution which wasn't a 'Session_start()' problem or similar.

 

Could it be a server side issue which Hostgator needs to deal with?

 

Regards

Maudise

Link to comment
Share on other sites

The output from your sessionstart.php file doesn't match the posted code above. Are you sure what's actually in that files? Could you post the sessionstart.php file again.

 

Are you using any foreign language characters (non ASCII) or any control/non-printing in the 'test' array index name, so that what is actually showing in your file isn't what is being shown in your posts in the forum? We have seen times where the copy/paste into the forum post produces 'clean' code that works, but there is something in the actual code file that doesn't match.

 

Try deleting the ['test'] portion of the $_SESSION variable and retype it.

 

When I visit your two pages, I get the same session id cookie value on both pages. That indicates that the session_start on both pages is starting/resuming the same session data, but the ['test'] index isn't present on the 2nd page. There may be an issue with the server where the file name is being created, but the actual file isn't (there is typically a php error when this type of thing occurs.)

Link to comment
Share on other sites

well, I genuinely have no idea why this is working now. I've gone back and changed the ['test'] to be ["test"] on all the variables and it seems to now be working.

 

Thank you very much for your help, if you have any great words of wisdom as to how to avoid this in the future [' '] wasn't picked up and it was copied from a tutorial, so I'm presuming you can use a single quotation on some systems?

 

Regards

Maudise

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.