|
-
Apr 9th, 2009, 03:33 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] keeping logged in username consistent on all pages.
Hello i have a registration and login form on my website.
In the top right corner or whereever on my website i want the following text to be present:
The 'Guest' should be replaced with the logged in user. This text will be on every page. How do i change the text so it is customized for the logged in user? I was thinking since PHP is server side if i were to make a change for one page it would make the same change for every user viewing the page.
How would i go about this?
-
Apr 9th, 2009, 04:03 PM
#2
Re: keeping logged in username consistent on all pages.
you use either sessions or cookies. sessions are for storing temporary information, like logins, and cookies are for storing long term information. you could use a combination of both, but for now I'll just show you how to set and use a session. sessions exist on the server side (whereas cookies exist on the client side) and will expire if a user hasn't been active on a page using sessions after 20 minutes (by default). sessions will also expire if the browser window is closed.
to tell PHP that you'll be using sessions, you will need to call the session_start() function on every page that will handle sessions. this function either starts a new session, or resumes an old session. you'll need to have this call on your login page and also on any page that should say, "welcome <username>." session_start() also sends headers, and so needs to be called BEFORE any output is sent. for example:
PHP Code:
//this works: session_start(); echo 'Hello!';
//while this will throw an error: echo 'Hello!'; session_start();
you can manipulate sessions by using the $_SESSION global array, after you've started your session. your login page would change to something like:
PHP Code:
<?php //start/resume our session session_start();
//I replaced all login logic with this line if($login == true){
//login is successful! echo 'you have successfully logged in!'; //set our sessions $_SESSION['username'] = $username; $_SESSION['loggedin'] = true; } ?>
and then our "main" page might look like:
PHP Code:
<?php //start/resume our session session_start();
//this will set $username to $_SESSION['username'] if it exists, otherwise, it will be set to Guest. $username = (isset($_SESSION['username'])) ? $_SESSION['username'] : "Guest";
echo "Welcome, {$username}!";
//we can also do this: if($_SESSION['loggedin']){ //do something for logged in users only here ?> <a href="logout.php">Logout <?php echo $username; ?></a> <?php } ?>
and, we can create logout.php:
PHP Code:
<?php //start/resume session session_start();
//destroy our login sessions unset($_SESSION['loggedin']); unset($_SESSION['username']);
//redirect the user to the main page now that everything has been unset header("Location: /index.php"); ?>
Last edited by kows; Apr 9th, 2009 at 04:07 PM.
-
Apr 10th, 2009, 10:17 AM
#3
Thread Starter
Frenzied Member
Re: [RESOLVED] keeping logged in username consistent on all pages.
Thank you this thread is resolved.
-
Apr 11th, 2009, 07:46 PM
#4
Thread Starter
Frenzied Member
Re: [RESOLVED] keeping logged in username consistent on all pages.
Well actually one more thing, for the logout.php i am getting this error:
Parse error: syntax error, unexpected T_VARIABLE in /home/a1401476/public_html/logout.php on line 3
Line 3 is the line that unsets the username session...
-
Apr 11th, 2009, 08:40 PM
#5
Re: [RESOLVED] keeping logged in username consistent on all pages.
it's a syntax error, so you're probably missing a parenthesis for unset() or a semicolon afterward. the logout.php I posted in this thread does not have any such errors; if you retyped it instead of copying/pasting, make sure you don't have any typos.
-
Apr 11th, 2009, 08:53 PM
#6
Thread Starter
Frenzied Member
Re: [RESOLVED] keeping logged in username consistent on all pages.
I copyied and pasted it, and once i recieved the error and copied and pasted it again and even retyped it.
Here is the full code direct from the file:
Code:
<?php
session_start()
unset($_SESSION['username']);
header("Location: /index.php");
?>
EDIT:
oh sorry i gave the wrong error, here is the "updated" error:
Parse error: syntax error, unexpected T_UNSET in /home/a1401476/public_html/logout.php on line 3
Last edited by noahssite; Apr 11th, 2009 at 08:58 PM.
-
Apr 11th, 2009, 10:30 PM
#7
Re: [RESOLVED] keeping logged in username consistent on all pages.
you weren't copying and pasting it correctly then, or you removed too much of it. you're missing a semicolon at the end of session_start(), something similar to what I had mentioned in my last post.
simple debugging like this should be trivial :)
-
Apr 12th, 2009, 03:42 PM
#8
Thread Starter
Frenzied Member
Re: [RESOLVED] keeping logged in username consistent on all pages.
oh.. well that really would result in error... thank you.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|