代码练习之 PHP会话控制 session cookie
log.html
<html> <head><title>Home Page</title></head> <body> <form action="login.php" method="post"> <input type="text" name="name"> <input type="submit" value="Log in"> </form> </body> </html>
login.php
<?php session_start(); if (isset($_POST[‘name‘])) { $name=$_POST[‘name‘]; $connect=@new mysqli(‘localhost‘,‘root‘,‘123‘,‘mytestdb‘); if (mysqli_connect_errno()) { echo "Connection to database failed:".mysqli_connect_errno(); exit; # code... } $query="select*from users where name=‘$name‘"; $result=$connect->query($query); if ($result->num_rows) { $_SESSION[‘name‘]=$name; # code... } $connect->close(); # code... } if (isset($_SESSION[‘name‘])) { echo "You are logged in as ".$_SESSION[‘name‘]."<br/>"; echo "<a href=‘logout.php‘>Log out</a>"."<br/>"; # code... }else{ if (isset($name)) { echo "Could not log in"."<br/>"; # code... }else{ echo "You are not logged in"."<br/>"; } }
logout.php
<?php session_start(); unset($_SESSION[‘name‘]); session_destroy(); if (empty($_SESSION[‘name‘])) { echo "You have log out!"; # code... }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。