PHP Login Status Display
Hi, I need a script that shows if you are logged.
if they are not, it says "Log In".
if they are, it says, "Log Out -username-"
Thanks in advance, this is very important.
All the best news here: https://newsbotnet.com
Hi, I need a script that shows if you are logged.
if they are not, it says "Log In".
if they are, it says, "Log Out -username-"
Thanks in advance, this is very important.
All the best news here: https://newsbotnet.com
kazimmerman posted this at 20:12 — 11th December 2007.
He has: 698 posts
Joined: Jul 2005
Well, this would require the use of cookies and/or sessions, and here's how I would most simply do it:
A log-in form would process to this page:
if username and password are verified {setcookie('giveItAName', $userNameVariable, $expiresWhen);
echo "You are now logged in.";
}
Then, on each page (I would place the code in a header that can be included on each page) you could see if the cookie exists:
if (isset($_COOKIE['giveItAName'])) {$makeItAVariable = $_COOKIE['giveItAName'];
echo "Log out -" . $makeItAVariable . "-";
}
else {
echo "Log in";
}
You could make something much more elaborate than this, but this is basically how it works.
Kurtis
DarkLight posted this at 20:39 — 11th December 2007.
He has: 287 posts
Joined: Oct 2007
I cannot seem to get it to work, here is my "checklogin.php" script...
<?php
$host=\"localhost\";
$username=\"USERNAME\";
$password=\"PASSWORD\";
$db_name=\"DATABASE\";
$tbl_name=\"TABLE\";
// Connect to server and select databse.
mysql_connect(\"$host\", \"$username\", \"$password\")or die(\"cannot connect\");
mysql_select_db(\"$db_name\")or die(\"cannot select DB\");
// username and password sent from signup form
$username=$_POST['username'];
$password=$_POST['password'];
$sql=\"SELECT * FROM $tbl_name WHERE username='$username' and password='$password'\";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file \"login_success.php\"
session_register(\"username\");
session_register(\"password\");
header('location:http://pcgenius.pcriot.com/members/basic/login/login_success.php');
}
else {
header('location:http://pcgenius.pcriot.com/members/basic/login/login_failed.php');
}
?>
What would I need to do to display the content of the created cookie on a different page?
Thanks you so much in advance, i am fed up of this now, i cannot seem to get the right script.
All the best news here: https://newsbotnet.com
kazimmerman posted this at 23:54 — 11th December 2007.
He has: 698 posts
Joined: Jul 2005
First of all, although I can't see it being the problem here, I would suggest creating variables with different names for the visitor's username and password and those for the MYSQL connection.
Also, I think you need the following line before you start registering values on a session:
session_start();And where you try to do this, I think you should be inserting the variable, correct?
session_register('$username');session_register('$password');
I'm not very experienced in using sessions, but this is how I would approach it based off of what I know.
Kurtis
DarkLight posted this at 22:26 — 12th December 2007.
He has: 287 posts
Joined: Oct 2007
AHHAH!!!
I sorted it.
all i did was add...
<?phpsession_start();
?>
to every page i wanted the code to work on, it simply reminds the browser that it has a session in progress. thanks for your help. Very much appreciated, it was your idea that gave me inspiration to do this.
All the best news here: https://newsbotnet.com
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.