Sessions

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
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Sessions

Post by icesolid »

Could someone show me some basic code for a log in using sessions.

Thank you.
MattF
Forum Contributor
Posts: 225
Joined: Sun May 19, 2002 9:58 am
Location: Sussex, UK

Post by MattF »

Here is some code, it can be modified to be on one page or several:

Login Form

<form action="page.php" method="post">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Login">

Proccess Login

if(mysql_num_rows(mysql_query("SELECT * FROM login_table WHERE username='".$_POST['username']."' AND password = '".$_POST['password']."'")) == 1) {
session_start();
$_SESSION['LOGGED_IN'] = 1;
echo "You are now logged in";
} else {
echo "Invalid username or password";
}

Check if someone is logged in

if($_SESSION['LOGGED_IN']==1) {
echo "You are logged in";
} else {
echo "Please log in";
}
Post Reply