PHP, Session ID, $_Session, Set Cookie
Video STOPPED HERE
https://youtu.be/04rWBt93NkY?t=1h30m32s
Course Web Site by CS75.tv
Course Sillabus (Opens in a PDF)
Course Sections Lead By TA
Course Projects
The course's own website is at www.cs75.net.
Google Discussion Group
PHP Manual
Lecture 0: HTTP |
Lecture 1: PHP |
Lecture 2: PHP, Continued |
Lecture 3: MVC, XML |
Lecture 4: SQL |
Lecture 5: SQL, Continued |
Lecture 6: JavaScript |
Lecture 7: Ajax |
Lecture 8: Security |
Lecture 9: Scalability |
Run PHP scripts on the XAMPP platform
I was accessing the PHP file as a file on c drive
(eg. C:\xamp\htdocs\froshims1.php or file:///c:/xamp/htdocs/froshims1.php)
instead of as a webpage (ie //localhost/froshims1.php).
This solved the problem.
Reference >> Solution to the problem with XAMPP not running a PHP file.
https://stackoverflow.com/questions/9406433/php-not-working-in-xampp
==========================================
<?php
/**
* home.php
*
* A simple home page for these login demos.
*
* David J. Malan
* malan@harvard.edu
*/
// enable sessions
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Home</h1>
<h3>
<?php if (isset($_SESSION["authenticated"]) && $_SESSION["authenticated"] === true) { ?>
You are logged in!
<br>
<a href="logout.php">log out</a>
<?php } else { ?>
You are not logged in!
<?php } ?>
</h3>
<br>
<b>Login Demos</b>
<ul>
<li><a href="login1.php">version 1</a></li>
<li><a href="login2.php">version 2</a></li>
<li><a href="login3.php">version 3</a></li>
<li><a href="login4.php">version 4</a></li>
</ul>
</body>
</html>
==========================================
<?
/**
* login3.php
*
* A simple login module that remembers username of most recently
* authenticated user so that it can pre-populate the username
* field next time.
*
* David J. Malan
* malan@harvard.edu
*/
// enable sessions
session_start();
// were this not a demo, these would be in some database
define("USER", "jharvard");
define("PASS", "crimson");
// if username and password were submitted, check them
if (isset($_POST["user"]) && isset($_POST["pass"]))
{
// if username and password are valid, log user in
if ($_POST["user"] == USER && $_POST["pass"] == PASS)
{
// remember that user's logged in
$_SESSION["authenticated"] = true;
// save username in cookie for a week
setcookie("user", $_POST["user"], time() + 7 * 24 * 60 * 60);
// redirect user to home page, using absolute path, per
// http://us2.php.net/manual/en/function.header.php
$host = $_SERVER["HTTP_HOST"];
$path = rtrim(dirname($_SERVER["PHP_SELF"]), "/\\");
header("Location: http://$host$path/home.php");
exit;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Log In</title>
</head>
<body>
<form action="<?= $_SERVER["PHP_SELF"] ?>" method="post">
<table>
<tr>
<td>Username:</td>
<td>
<? if (isset($_POST["user"])): ?>
<input name="user" type="text" value="<?= htmlspecialchars($_POST["user"]) ?>">
<? elseif (isset($_COOKIE["user"])): ?>
<input name="user" type="text" value="<?= htmlspecialchars($_COOKIE["user"]) ?>">
<? else: ?>
<input name="user" type="text" value="">
<? endif ?>
</tr>
<tr>
<td>Password:</td>
<td><input name="pass" type="password"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Log In"></td>
</tr>
</table>
</form>
</body>
</html>
==========================================
<?
/**
* logout.php
*
* A simple logout module for all of our login modules.
*
* David J. Malan
* malan@harvard.edu
*/
// enable sessions
session_start();
// delete cookies, if any
setcookie("user", "", time() - 3600);
setcookie("pass", "", time() - 3600);
// log user out
setcookie(session_name(), "", time() - 3600);
session_destroy();
?>
<!DOCTYPE html>
<html>
<head>
<title>Log Out</title>
</head>
<body>
<h1>You are logged out!</h1>
<h3><a href="home.php">home</a></h3>
</body>
</html>
==========================================
<?php
/**
* home.php
*
* A simple home page for these login demos.
*
* David J. Malan
* malan@harvard.edu
*/
// enable sessions
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Home</h1>
<h3>
<?php if (isset($_SESSION["authenticated"]) && $_SESSION["authenticated"] === true) { ?>
You are logged in!
<br>
<a href="logout.php">log out</a>
<?php } else { ?>
You are not logged in!
<?php } ?>
</h3>
<br>
<b>Login Demos</b>
<ul>
<li><a href="login1.php">version 1</a></li>
<li><a href="login2.php">version 2</a></li>
<li><a href="login3.php">version 3</a></li>
<li><a href="login4.php">version 4</a></li>
</ul>
</body>
</html>
==========================================
<?
/**
* login3.php
*
* A simple login module that remembers username of most recently
* authenticated user so that it can pre-populate the username
* field next time.
*
* David J. Malan
* malan@harvard.edu
*/
// enable sessions
session_start();
// were this not a demo, these would be in some database
define("USER", "jharvard");
define("PASS", "crimson");
// if username and password were submitted, check them
if (isset($_POST["user"]) && isset($_POST["pass"]))
{
// if username and password are valid, log user in
if ($_POST["user"] == USER && $_POST["pass"] == PASS)
{
// remember that user's logged in
$_SESSION["authenticated"] = true;
// save username in cookie for a week
setcookie("user", $_POST["user"], time() + 7 * 24 * 60 * 60);
// redirect user to home page, using absolute path, per
// http://us2.php.net/manual/en/function.header.php
$host = $_SERVER["HTTP_HOST"];
$path = rtrim(dirname($_SERVER["PHP_SELF"]), "/\\");
header("Location: http://$host$path/home.php");
exit;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Log In</title>
</head>
<body>
<form action="<?= $_SERVER["PHP_SELF"] ?>" method="post">
<table>
<tr>
<td>Username:</td>
<td>
<? if (isset($_POST["user"])): ?>
<input name="user" type="text" value="<?= htmlspecialchars($_POST["user"]) ?>">
<? elseif (isset($_COOKIE["user"])): ?>
<input name="user" type="text" value="<?= htmlspecialchars($_COOKIE["user"]) ?>">
<? else: ?>
<input name="user" type="text" value="">
<? endif ?>
</tr>
<tr>
<td>Password:</td>
<td><input name="pass" type="password"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Log In"></td>
</tr>
</table>
</form>
</body>
</html>
==========================================
<?
/**
* logout.php
*
* A simple logout module for all of our login modules.
*
* David J. Malan
* malan@harvard.edu
*/
// enable sessions
session_start();
// delete cookies, if any
setcookie("user", "", time() - 3600);
setcookie("pass", "", time() - 3600);
// log user out
setcookie(session_name(), "", time() - 3600);
session_destroy();
?>
<!DOCTYPE html>
<html>
<head>
<title>Log Out</title>
</head>
<body>
<h1>You are logged out!</h1>
<h3><a href="home.php">home</a></h3>
</body>
</html>
==========================================
Header Info;
Set-Cookie: PHPSESSID=
Header Info
Cookie: PHPSESSID=
Set Cookie
Inside cookie
authenticated|b:1;
Inside cookie
authenticated|b:1;user|s:8:"jharvard";
No comments:
Post a Comment