theboyholty Posted December 30, 2007 Share Posted December 30, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/83713-solved-sessions-and-maintaining-a-variable-throughout-a-session/ Share on other sites More sharing options...
avo Posted December 30, 2007 Share Posted December 30, 2007 Hi Lets have a look at the code you have so far maybe will be able to help you a little better. Regards Quote Link to comment https://forums.phpfreaks.com/topic/83713-solved-sessions-and-maintaining-a-variable-throughout-a-session/#findComment-425940 Share on other sites More sharing options...
papaface Posted December 30, 2007 Share Posted December 30, 2007 When they login: session_start(); $_SESSION['username'] = $username; echo $_SESSION['username']; Quote Link to comment https://forums.phpfreaks.com/topic/83713-solved-sessions-and-maintaining-a-variable-throughout-a-session/#findComment-425942 Share on other sites More sharing options...
avo Posted December 30, 2007 Share Posted December 30, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/83713-solved-sessions-and-maintaining-a-variable-throughout-a-session/#findComment-425951 Share on other sites More sharing options...
papaface Posted December 30, 2007 Share Posted December 30, 2007 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.) Quote Link to comment https://forums.phpfreaks.com/topic/83713-solved-sessions-and-maintaining-a-variable-throughout-a-session/#findComment-425967 Share on other sites More sharing options...
avo Posted December 30, 2007 Share Posted December 30, 2007 instead of picking help the guy Quote Link to comment https://forums.phpfreaks.com/topic/83713-solved-sessions-and-maintaining-a-variable-throughout-a-session/#findComment-425971 Share on other sites More sharing options...
papaface Posted December 30, 2007 Share Posted December 30, 2007 instead of picking help the guy I already did. It is you that didn't. You told him code that is not even correct at all. Quote Link to comment https://forums.phpfreaks.com/topic/83713-solved-sessions-and-maintaining-a-variable-throughout-a-session/#findComment-425972 Share on other sites More sharing options...
theboyholty Posted December 30, 2007 Author Share Posted December 30, 2007 Thanks chaps, its all sorted now. Many thanks to you both. Quote Link to comment https://forums.phpfreaks.com/topic/83713-solved-sessions-and-maintaining-a-variable-throughout-a-session/#findComment-425986 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.