PHP Code:
session_start();
mysql_connect("localhost", "username", "password");
mysql_select_db("some_db");
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT id FROM users WHERE username = '" . mysql_real_escape_string($username) . "' AND password = '" . mysql_real_escape_string($password) . "'";
$result = mysql_query($sql);
if (mysql_num_rows($result) == 0) {
echo "Username and password combination not found.";
} else {
$row = mysql_fetch_array($result);
$_SESSION['logged_in'] = 1;
$_SESSION['user_id'] = $row['id'];
header("Location: http://www.example.com/member-area.php");
}
On your pages that require authorization, check for the presence of those session values:
PHP Code:
session_start();
if (!isset($_SESSION['logged_in'])) {
//Not logged in, redirect to login page
header("http://www.example.com/login.php");
}
//If they pass that, they're logged in, and are authorized to view whatever else is in this file
Bookmarks