php登录实例代码

1 login.php

<?php
// $conn = mysql_connect("localhost", "root", "root") or die("Could not connect mysql!".msyql_error()."<br />");
// mysql_query("create database cookie_user") or die("Create database failed!".mysql_error()."<br />");
// mysql_select_db("cookie_user");
/* $sql = "create table user(
		id int(10) not null auto_increment,
		name varchar(50) default null,
		password varchar(50) default null,
		age int(5) default null,
		primary key(id)
		)engine=Innodb default charset=utf8";//注意不是utf-8
mysql_query($sql, $conn); 
mysql_query("insert into user values(null, 'admin', md5('123456'), 11)");
mysql_query("insert into user values(null, 'user', md5('123456'), 11)");
mysql_query("insert into user values(null, 'hello', md5('123456'), 11)");
$result = mysql_query("select * from user");*/
// include dirname(__FILE__).'/com.php';
// include dirname(__FILE__).'/conn.php';

include 'com.php';
include 'conn.php';

// echo dirname(__FILE__);
if(isset($_POST['sub'])){
	$name = $_POST['name'];
	$password = md5($_POST['password']);
// 	echo $password;
// 	echo $name;
	$sql = mysql_query("select * from user where name = '$name' and password = '$password' ");//注意后面的name一定要加'',否则无效
// 	$sql = mysql_query("select * from user");
// 打印sql查询结果	
/*  	while($row = mysql_fetch_array($sql)){
		print_r($row['name']."<br />");
	}   */
	
/*  	$row = mysql_fetch_array($sql);
	print_r($row['name'].$row['password']);  */
	
	while($row = mysql_fetch_array($sql)){
		$time = time() + 3600;
		setCookie("username", $row['name'], $time);
		setCookie("uid", $row['id'], $time);
		setCookie("age", $row['age'], $time);
		setCookie("isLogin", true, $time);
	}
	
	header("Location:index.php");
}

?>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>login.php</title>
	</head>
	<body>
		<div>
			<form action="" method="post"><!-- action中无参数则默认跳转到本页 -->
				<table border="1" align="center">
					<caption>User login</caption>
					<tr>
						<td>username</td>
						<td><input type="text" name="name" /></td>
					</tr>
					<tr>
						<td>password</td>
						<td><input type="password" name="password" /></td>
					</tr>
					<tr>
						<td colspan="1"></td>
						<td><input type="submit" name="sub" value="submit" /></td>
					</tr>
				</table>
			</form>
		</div>
	</body>
</html>

2 index.php

<?php
include com.php;
echo "User, ".$_COOKIE['username'].", welcome!";
echo "Your id is ".$_COOKIE['uid']."<br />";
echo "This is the first page.";
?>

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>index.php</title>
	</head>
	<body>
		<div>
			<a href="secondPage.php">second page</a>
			<a href="thirdPage.php">third page</a>
			<a href="logout.php">logout</a>
		</div>
	</body>
</html>

3 com.php

<?php
if(!$_COOKIE['isLogin']){
	header("Location:login.php");
	echo "Please login first!";
}

4 conn.php

<?php
$conn = mysql_connect("localhost", "root", "root") or die("Could not connect mysql!".msyql_error()."<br />");
mysql_select_db("cookie_user");
?>

5 secondPage.php

<?php
include com.php;
echo "User, ".$_COOKIE['username'].", welcome!";
echo "Your id is ".$_COOKIE['uid']."<br />";
echo "This is the second page.";
?>

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>index.php</title>
	</head>
	<body>
		<div>
			<a href="secondPage.php">second page</a>
			<a href="thirdPage.php">third page</a>
			<a href="logout.php">logout</a>
		</div>
	</body>
</html>

6 thirdPage.php

<?php
include com.php;
echo "User, ".$_COOKIE['username'].", welcome!";
echo "Your id is ".$_COOKIE['uid']."<br />";
echo "This is the third page.";
?>

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>index.php</title>
	</head>
	<body>
		<div>
			<a href="secondPage.php">second page</a>
			<a href="thirdPage.php">third page</a>
			<a href="logout.php">logout</a>
		</div>
	</body>
</html>

你可能感兴趣的:(php登录实例代码)