note 127639 added to book.session
| From: | php dot user1577 at gmail dot com | Date: | Thu, 15 Sep 2022 21:46:11 +0000 |
| Subject: | note 127639 added to book.session | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-219583@lists.php.net to get a copy of this message | ||
// Some simple script snippets for session processing
// ---------- Page - login.php ----------
<?php
session_start();
require "./LogonClass.php";
if (isset($_POST['submit'])) {
LoginClass::loginIfUserTrue();
}
LoginClass::passThroughIfLoggedIn();
?>
// ... other HTML Code
// ---------- LoginClass.php ----------
<?php
class LoginClass {
public static function loginIfUserTrue() {
$data = DatabaseClass::getUserByEmail($_POST['email']);
if (empty($data)) {
echo "<tr><td><p>User not
found</p></td></tr>";
}
$idDatabase = $data['Id'];
$nameDatabase = $data['Name'];
$emailDatabase = $data['Email'];
$passwordDatabase = $data['Password'];
if (isset($_POST['email']) and $_POST['email'] != "" and
isset($_POST['password']) and $_POST['password'] != "") {
if ($_POST['email'] == $emailDatabase and $_POST['password'] ==
$passwordDatabase) {
$_SESSION['email'] = $_POST['email'];
$_SESSION['loggedin'] = true;
header("Location: welcome.php");
} else {
echo "<tr><td><p>User, Password
wrong</p></td></tr>";
$_SESSION['loggedin'] = false;
}
}
}
public static function passThroughIfLoggedIn() {
if (isset($_SESSION['loggedin']) and $_SESSION['loggedin'] == true) {
header("Location: welcome.php");
}
}
public static function logout() {
if (isset($_POST['logout']) and $_POST['logout'] != "") {
if ($_POST['logout'] == "loggedout") {
session_destroy();
header("Location: login.php");
}
}
}
// Example for a force logout, if the user role in Session not right (customer or staff)
// You need a variable $userRole in Session
// Call the method as first in relevant pages
public static function logoutForceIfUserRole($userRole) {
if (!isset($_SESSION['userRole']) or $_SESSION['userRole'] == $userRole)
{
session_destroy();
header("Location: login.php");
}
}
}
?>
// ---------- DatabaseClass.php ----------
<?php
class DatabaseClass {
private $userEmail = '';
public static function connect() {
$dbName = 'MyDatabase';
$dbHost = 'localhost';
$dbUsername = 'root';
$dbUserPassword = '';
try {
$conn = new PDO("mysql:host=$dbHost;dbname=$dbName;", $dbUsername,
$dbUserPassword);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
return $conn;
}
public static function getUserByEmail($userEmail) {
$db = DatabaseClass::connect();
$sql="SELECT * FROM Users WHERE Email = ?";
$stmt = $db->prepare($sql);
$stmt->execute([$userEmail]);
$data = $stmt->fetch(PDO::FETCH_ASSOC);
$db = null;
return $data;
}
}
?>
----
Server IP: 45.112.84.4
Probable Submitter: 185.85.3.173 (proxied: 185.17.14.59)
----
Manual Page -- https://php.net/manual/en/book.session.php
Edit -- https://main.php.net/note/edit/127639
Del: integrated -- https://main.php.net/note/delete/127639/integrated
Del: useless -- https://main.php.net/note/delete/127639/useless
Del: bad code -- https://main.php.net/note/delete/127639/bad+code
Del: spam -- https://main.php.net/note/delete/127639/spam
Del: non-english -- https://main.php.net/note/delete/127639/non-english
Del: in docs -- https://main.php.net/note/delete/127639/in+docs
Del: other reasons-- https://main.php.net/note/delete/127639
Reject -- https://main.php.net/note/reject/127639
Search -- https://main.php.net/manage/user-notes.php