Blogger Information
Blog 250
fans 3
comment 0
visits 321737
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP sesslion小案例
梁凯达的博客
Original
730 people have browsed it

实例

<?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');

运行实例 »

点击 "运行实例" 按钮查看在线实例

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post