Several predefined variables in PHP are "superglobals", which means they are available in all scopes throughout a script. There is no need to do global $variable; to access them within functions or methods.
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).
========================================== <?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>