0% found this document useful (0 votes)
3 views3 pages

Session&Cookies Example

The document outlines a PHP login system using sessions and cookies, detailing the flow from login to logout. It includes code examples for three main pages: login.php, welcome.php, and logout.php, explaining how user credentials are managed and stored. The system checks for existing cookies, processes login submissions, and allows users to log out, effectively managing user sessions and cookies.

Uploaded by

deshardilasha879
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Session&Cookies Example

The document outlines a PHP login system using sessions and cookies, detailing the flow from login to logout. It includes code examples for three main pages: login.php, welcome.php, and logout.php, explaining how user credentials are managed and stored. The system checks for existing cookies, processes login submissions, and allows users to log out, effectively managing user sessions and cookies.

Uploaded by

deshardilasha879
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PHP Login System (Sessions & Cookies Example)

How It Works (Overall Flow)

1. User visits [Link].


2. If already logged in (cookie exists) → redirected to [Link].
3. User submits the login form → username is stored in session (server) and cookie
(browser).
4. User sees a welcome message with both session and cookie info.
5. Clicking logout → removes session and cookie → user can log in again.

Example: Create a [Link] page at first.

<?php

// Check if user is already logged in via cookie


if(isset($_COOKIE['username'])) {
header("Location: [Link]"); // redirect to welcome page
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h2>Login</h2>
<form method="post" action="[Link]">
Username: <input type="text" name="username" required><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
Example: Create a [Link] page then.

<?php
// [Link]
session_start(); // start session

// If coming from login form


if(isset($_POST['username'])) {
$username = $_POST['username'];

// Store username in session (server-side)


$_SESSION['username'] = $username;

// Store username in cookie (client-side, expires in 1 day)


setcookie("username", $username, time() + 86400);

header("Location: [Link]");
exit();
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>

<h2>
Welcome (Cookie):
<?php
echo isset($_COOKIE['username'])
? htmlspecialchars($_COOKIE['username'])
: 'Guest';
?>
</h2>

<h2>
Welcome (Session):
<?php
echo isset($_SESSION['username'])
? htmlspecialchars($_SESSION['username'])
: 'Guest';

echo "<br><small>(Session ID: " . session_id() . ")</small>";


?>
</h2>

<a href="[Link]">Logout</a>

</body>
</html>
Example: Create a [Link] page at last.

<?php
// [Link]
session_start();

// Destroy session
session_unset();
session_destroy();

// Remove cookie
setcookie("username", "", time() - 3600);

echo "You have been logged out.<br>";


echo "<a href='[Link]'>Login again</a>";
?>

What This System Does and How It Works

1. Login Page ([Link])


o Displays a login form asking for a username.
o Checks if a user already has a username cookie; if yes, automatically redirects
them to the welcome page.

2. Login Processing & Welcome Page ([Link])


o Accepts the username submitted from the login form.
o Stores the username in both:
§ Session → stored on the server and lasts until the user logs out or closes
the browser.
§ Cookie → stored in the browser and lasts 1 day by default.
o Displays a welcome message showing the username from both the cookie and the
session.
o Shows the session ID for reference/debugging.

3. Logout Page ([Link])


o Ends the session by removing all server-side session variables.
o Deletes the username cookie from the browser.
o Shows a message confirming the logout and provides a link to log in again.

You might also like