Jump to content

[SOLVED] Sessions, and maintaining a variable throughout a session


theboyholty

Recommended Posts

I am developing a message board and I want my users to log in before they can create threads or post replies. I have created a successful logging in section and a session is created when they enter their username and password but when they come to post a message, I want to be able to pick up their username automatically from the ongoing session they created.

 

What I want to do, therefore, is to somehow make the session record their username, so this can be dragged up from any page of the website at will so when they post messages they will be attributed to the current logged in user.

 

I have a PHP book (Sitepoint/Kevin Yank) which seems to suggest that I can change the session name to be the name of a variable ($username for example) but i can't get it to work.

 

I'm having real trouble with this, can anyone advise please?

 

Link to comment
Share on other sites

Hi

 

This session will be held on the clients browser until it is destroyed or the browser is closed.

 

so as long as you use

 

$session_start(); on every page before all your code

 

then as you have done already

<?=$session['username']?> ; or echo $session['username'] (both the same)

 

this will print to screen the username.

 

hope this helps and i have understood correct for you.

Link to comment
Share on other sites

Hi

 

This session will be held on the clients browser until it is destroyed or the browser is closed.

 

so as long as you use

 

$session_start(); on every page before all your code

 

then as you have done already

<?=$session['username']?> ; or echo $session['username'] (both the same)

 

this will print to screen the username.

 

hope this helps and i have understood correct for you.

Your code is wrong.

$session_start(); on every page before all your code 

Should be:

session_start(); on every page before all your code 

and

<?=$session['username']?> ; or echo $session['username'] (both the same)

should be:

<?=$_SESSION['username'];?> or echo $_SESSION['username']; (both the same)

:-\

 

P.S (Browsers do not hold sessions, sessions are server side, cookies are client side.)

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.