日期:2014-05-17 浏览次数:20451 次
<?php //会话的第2个例子,首页 //9.php,简单例子,首页 //10.php,简单例子,登录页 //11quit.php,简单例子,退出页 //19.php, 复杂例子,首页 //20.php, 复杂例子,登录页 //21quit.php, 复杂例子,退出页 require_once('../Public/CommandLine.php'); session_start(); //逻辑是如果未登录,则显示“请登录”,如果已登录,显示欢迎某某某 $html = ''; if (isset($_SESSION['islogin']) && $_SESSION['islogin'] == 1 ) { //已登录 $html .= '欢迎您,' . $_SESSION['username'] . " <a href='11quit.php'>退出</a>"; } else { //未登录分:有cookie的记住我,且为1 if (isset($_COOKIE['rememberme']) && $_COOKIE['rememberme']==1 ) { $username = strval($_COOKIE['username']); $password = strval($_COOKIE['password']); $db = Sys::getdb(); $select = $db->select(); $select->from('test.member', 'count(*)' ) ->where('username=?', $username) ->where('password=?', $password); $result = $db->fetchOne($select); if ($result) { //如果是对的 $_SESSION['islogin'] = 1; $_SESSION['username'] = $username; $html .= '欢迎您,' . $_SESSION['username'] . " <a href='21quit.php'>退出</a>"; } } } if(!( isset($_SESSION['islogin']) && $_SESSION['islogin'] == 1 ) ){ $html .= '<a href="20.php">请登录</a>'; } $html .= "<br><h1>这是首页<h1>"; echo $html;
<?php //自动登录的登录页 require_once('../Public/CommandLine.php'); session_start(); $isget = ($_SERVER['REQUEST_METHOD'] == 'GET') ? 1 : 0; if ($isget) { $html = <<<longs <html> <body> <form method="post"> 请输入用户名:<input type="text" name="username" value=''><br> 请输入密码:<input type="text" name="password" value=''><br> <input type="checkbox" name="rememberme" value="1" />下次自动登录<br> <input type="submit" value="登录" > </form> </body> </html> longs; echo $html; } else { //echo $_POST['name1']; $username = $_POST['username']; $password = $_POST['password']; $db = Sys::getdb(); $select = $db->select(); $select->from('test.member', 'count(*)' ) ->where('username=?', $username) ->where('password=?', md5($password)); $result = $db->fetchOne($select); if ($result) { $_SESSION['islogin'] = 1; $_SESSION['username'] = $username; $time = time() + 3600 * 24 * 365; setco