PHP Login Form with Sessions

您所在的位置:网站首页 脑梗饮食怎么吃 PHP Login Form with Sessions

PHP Login Form with Sessions

2023-04-25 20:18| 来源: 网络整理| 查看: 265

Hi Im trying to get the username of the logged in user displayed, i have implemented sessions but to no avail. Once the user logs in at first it displays the message you are logged in as “Eddy Kay” for instance, but once you click another link the username vanishes. Could you tell me what I am doing wrong please.

Here is the code below. User_User.class.php class User_User { public function __construct(){ //start a session session_start();

}

public function isLoggedIn(){ $sessionIsSet = isset( $_SESSION[‘username’]);

if ( $sessionIsSet ) {

$out = $_SESSION[‘username’]; } else { $out = false; } return $out; }

public function login () { //set session variable [‘logged_in’] to true $_SESSION[‘username’] = true; }

public function logout () { //set session variable [‘logged_in’] to false $_SESSION[‘username’] = false; }

}

User_Table.class.php checkEmail( $email ); $this->checkUsername($username); //encrypt password with MD5 $sql = “INSERT INTO users (email, firstname, lastname, username, password) VALUES(?, ?, ?, ?, MD5(?))”; $data= array( $email, $firstname, $lastname, $username, $password ); $this->makeStatement( $sql, $data ); }

private function checkEmail ($email) { $sql = “SELECT email FROM users WHERE email = ?”; $data = array( $email ); $this->makeStatement( $sql, $data ); $statement = $this->makeStatement( $sql, $data ); //if a user with that e-mail is found in database if ( $statement->rowCount() === 1 ) { //throw an exception > do NOT create new admin user $e = new Exception(“Error: ‘$email’ already used!”); throw $e; } } private function checkUsername ($username) { $sql = “SELECT username FROM users WHERE username = ?”; $data = array( $username ); $this->makeStatement( $sql, $data ); $statement = $this->makeStatement( $sql, $data ); //if a user with that e-mail is found in database if ( $statement->rowCount() === 1 ) { //throw an exception > do NOT create new admin user $e = new Exception(“Error: ‘$username’ already used!”); throw $e; } }

public function checkCredentials ( $username, $password ){ $sql = “SELECT username FROM users WHERE username = ? AND password = MD5(?)”; $data = array($username, $password); $statement = $this->makeStatement( $sql, $data ); if ( $statement->rowCount() === 1 ) { $out = true; } else { $loginProblem = new Exception( “Username/Password incorrect” ); throw $loginProblem; } return $out; } public function getUserName($username) { $sql = “SELECT username FROM users WHERE username = ? “; $data = array($username); $statement = $this->makeStatement($sql, $data);

return $statement; }

} ?>

login.php

checkCredentials( $username, $password ); $userTable->getUserName($username); $user->login();

} catch ( Exception $e ) { //login failed } //end of code changes

}

$loggingOut = isset ( $_POST[‘logout’] ); if ( $loggingOut ){ $user->logout();

}

if($user->isLoggedIn()) {

$view = include_once “views/logout-form-html.php”; } else { $view = include_once “views/login-form-html.php”; }

return $view; ?>

login-form-html.php

Thank you.



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3