PHP Login Function
Here is a function in PHP that checks if a user is logged in and if a login attempt has been submitted (and therefore logs them in if verified).
function check_login($username, $password, $required = 1, $redirect = 'https://www.domain.com/login.php') { if (isset($_SESSION['user']) AND isset($_SESSION['pass'])) return true; if (!empty($username) AND !empty($password)) { $username = stripslashes($username); $password = stripslashes($password); $checkq = mysql_query(" SELECT username, password FROM users WHERE username = '" . mysql_real_escape_string($username) . "' AND password = '" . md5($password) . "' LIMIT 1 "); if (mysql_num_rows($checkq) > 0) { $session_backup = $_SESSION; unset($_COOKIE[session_name()]); session_destroy(); session_start(); $_SESSION = $session_backup; unset($session_backup); $user_row = mysql_fetch_assoc($checkq); $_SESSION['user'] = $user_row['username']; $_SESSION['pass'] = $user_row['password']; unset($user_row); return true; } } if ($required == 1) { header("location: $redirect"); exit(); } return false; }

















