12c02-1.php

来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 69 行

PHP
69
字号
<?php// The login page -- First check if the user has requested to log-off$error = '';session_start();if (isset($_GET['logoff'])) {    // We need to completely destroy the session.  First the data:    $_SESSION = array();    // If a session cookie exists, tell the browser to destroy it     //  (give it a time in the past)    if (isset($_COOKIE[session_name()])) {        setcookie(session_name(), '', time()-1000, '/');    }    // Finally, finialize the session destruction:    session_destroy();}// Secondly, see if the user is already logged in.elseif (isset($_SESSION['valid']) && $_SESSION['valid']) {    // Bounce them to the view page://    header('Location: view.php');    header('Location: 12c02-2.php');    exit();}// 3rd, see if they attempted to log in:elseif (isset($_POST['user']) || isset($_POST['pass'])) {    // Just allowing a couple of fake users here, in real life you would    //  probably be reading this information from a database.    $user = isset($_POST['user']) ? $_POST['user'] : '';    $pass = isset($_POST['pass']) ? $_POST['pass'] : '';    if ( ( ($user == 'tanderson') && ($pass == 'Z1ON0101') ) ||         ( ($user == 'asmith') && ($pass == 'brawl') ) ) {        // user/pass is good, Store this fact in the session        $_SESSION['valid'] = 1;        $_SESSION['user'] = $_POST['user'];        // and direct them to the main page//        header('Location: view.php');        header('Location: 12c02-2.php');        exit();    } else {        // Prepare an error statement:        $error = 'Username & Password do not match, please try again';    }}// Otherwise, let's ask them to log in:?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Login</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><style>form { border: 1px solid black; padding: 10px; }#error { color: #FF0000; font-weight: bold; }</style></head><body><?php // If we had an error:if ($error) { echo "<p id=\"error\">{$error}</p>\n"; }?><form action="<?= $_SERVER['PHP_SELF'] ?>" method="post"><p>Welcome to our website, you need to log in:</p><p>Username: <input type="text" name="user" value="<?= @$user ?>"><br />Password: <input type="password" name="pass" value="<?= @$pass ?>"></p><p><input type="submit" value="Login" /></p></form></body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?