Losing Session Variable, Kinda

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
User avatar
tmaiden
Forum Commoner
Posts: 64
Joined: Fri Feb 24, 2006 3:15 pm
Location: Philadelphia
Contact:

Losing Session Variable, Kinda

Post by tmaiden »

I don't understand really how session variables work so prior to explaining my problem; I'll let you know where I currently stand.

I setup my site to use templates. At the top of each page there are includes, to include the headers, logon form, and navigation tabs.

1. I included session_start in my header template right before it sends anything to the browser.

2. After the header loads, then I include "templates\forms\logon.php" this is a form that allows the user to login. Once the logon is successful, $_SESSION['username'] = $_POST['username'].

Now that 1 and 2 are both done,
I show a form to the user to enter stuff, when I go to enter stuff into the database; I added this.
$requester = $_SESSION['username'];

It keeps return NULL!?!?

Now the weird thing is that what I stated above, 2., shows "Welcome tmaiden"
It pulls the tmaiden from $_SESSION['username']; in a seperate file "templates\forms\logon.php"

But when "templates\forms\logon.php" is loaded on the page with the form to enter other stuff, $_SESSION['username'] returns NULL

Any suggestions?
THanks.
User avatar
tmaiden
Forum Commoner
Posts: 64
Joined: Fri Feb 24, 2006 3:15 pm
Location: Philadelphia
Contact:

Post by tmaiden »

WTF!

I now added a text box,
<input type="text" name="test" value="<? echo $_SESSION['username']; ?>"><br/>

And it shows the user name; but yet this doesn't work

Code: Select all

if(isset($_SESSION['username'])){
				$requester = $_SESSION['username'];
			}
			else{
				$requester = 'Anonymous';
			}
			$query = "INSERT INTO ff_people (name, requester, status, ranking, timestamp)
			VALUES('" . $personname . "', '" . $requester . "', 0, 0, Now())";

			mysql_query($query) or die(mysql_error());
			mysql_close();
User avatar
tmaiden
Forum Commoner
Posts: 64
Joined: Fri Feb 24, 2006 3:15 pm
Location: Philadelphia
Contact:

Post by tmaiden »

Now this works.

Code: Select all

<input type="hidden" name="requester" value="<? echo $_SESSION['username']; ?>">
....
			if(isset($_POST['requester'])){
				$requester = $_POST['requester'];
			}
			else{
				$requester = 'Anonymous';
			}
			$query = "INSERT INTO ff_people (name, requester, status, ranking, timestamp)
			VALUES('" . $personname . "', '" . $requester . "', 0, 0, Now())";
But why doesn't this work?

Code: Select all

if(isset($_SESSION['username'])){
				$requester = $_SESSION['username'];
			}
			else{
				$requester = 'Anonymous';
			}
			$query = "INSERT INTO ff_people (name, requester, status, ranking, timestamp)
			VALUES('" . $personname . "', '" . $requester . "', 0, 0, Now())";
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Do you Have sesson_start(); at the top of every page?

I usually have a page called header.php or something similar, and just include that on the top of every page.

Then in there i have stuff like session_start(); etc..

-NSF
User avatar
tmaiden
Forum Commoner
Posts: 64
Joined: Fri Feb 24, 2006 3:15 pm
Location: Philadelphia
Contact:

Post by tmaiden »

Yes I do, thanks.
Post Reply