<?php //开启session session_start(); //var_dump($_SESSION); if(empty($_SESSION['admin'])){ echo '<a href="login.html">登录</a>|<a href="">注册</a>'; }else{ echo '欢迎'.$_SESSION['admin']['name'].'光临ss34破网站|<a href="./outlog.php">退出</a>'; }
点击 "运行实例" 按钮查看在线实例
<?php session_start(); var_dump($_POST); $name = $_POST['username']; $password = md5($_POST['password']); $link = mysqli_connect('localhost','root','123456'); if (mysqli_connect_errno($link)>0) { echo mysqli_connect_error($link);exit; } mysqli_select_db($link,'ss34'); mysqli_set_charset($link,'utf8'); $sql="SELECT * FROM myuser WHERE name='{$name}' and password='{$password}'"; $result = mysqli_query($link,$sql); if($result && mysqli_num_rows($result)>0){ //将得到的内容存储在session中除密码以外 $row = mysqli_fetch_assoc($result); //var_dump($row); //销毁密码 unset($row['password']); //var_dump($row); //将没有密码的值赋值给session即可 $_SESSION['admin']=$row; //var_dump($_SESSION); echo '登录成功'; echo '<a href="index.php">返回</a>'; }else{ echo '用户名或密码不正确!'; }
点击 "运行实例" 按钮查看在线实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <form action="dologin.php" method="post"> 用户名: <input type="text" name="username"><br/> 密码: <input type="password" name="password"><br/> <input type="submit" value="登录"> </form> </body> </html>
点击 "运行实例" 按钮查看在线实例
<?php session_start(); unset($_SESSION['admin']); header('location:index.php');
点击 "运行实例" 按钮查看在线实例