前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >php登录实例代码

php登录实例代码

作者头像
闵开慧
发布2018-03-30 13:59:20
3.7K0
发布2018-03-30 13:59:20
举报
文章被收录于专栏:闵开慧闵开慧
代码语言:javascript
复制
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>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档