Just Trying Hands in PHP 从 web 复制了一个登录脚本。但是它将进入 logincheck.php 页面,显示为空白。这是完整的脚本
登录.html
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="" />
<title>Simple login</title>
</head>
<body>
<div id="login">
<h2>Simple login with PHP MYSQL</h2>
<form action="logincheck.php" method="post">
<p> Username <label><input type="text" name="uname" /></label></p>
<p> Password <label><input type="password" name="passwd" /></label></p>
<label><input type="submit" name="submit" value="Login" /></label>
</form>
</div>
</body>
</html>
登录检查.php
<?php
/**
* @author
* @copyright 2013
*/
$con=mysql_connect("localhost","root","")or die("Cannot connect to databases!");
mysql_select_db("fakebook",$con);
$u=$_POST['uname'];
$p=md5($_POST['passwd']);
$query=mysql_query("
select * from users where username='$u' and password='$p'
");
$row=mysql_num_rows($query);
if ($row == 1)
{
session_start();
$a=mysql_fetch_array($query);
$_SESSION['user']=$a['username'];
header("location: home.php");
}
else
{
header("location: login.html");
}
?>
主页.php
<?php
/**
* @author
* @copyright 2013
*/
session_start();
if (isset($_SESSION['user'])){
echo "Welcome,".$_SESSION['user']."<br />
[ <a href=\"logout.php\">Logout</a> ]";
}else{
echo "You are not authorized into this page!";
}
?>
注销.php
<?php
/**
* @author
* @copyright 2013
*/
session_start();
if (isset($_SESSION['user'])){
session_destroy();
header("location: login.html");
}
else{
header("location: login.html");
}
?>
数据库名:fakebook,表名:users