Home > Backend Development > PHP Tutorial > Cookies must be enabled to log in php cookie login verification sample code

Cookies must be enabled to log in php cookie login verification sample code

WBOY
Release: 2016-07-29 08:39:36
Original
1240 people have browsed it

复制代码 代码如下:




Login














用户名:
密码:
Cookie保存时间:









复制代码 代码如下:


@mysql_connect("localhost", "root","1981427") //选择数据库之前需要先连接数据库服务器
or die("数据库服务器连接失败");
@mysql_select_db("test") //选择数据库mydb
or die("数据库不存在或不可用");
//获取用户输入
$username = $_POST['username'];
$passcode = $_POST['passcode'];
$cookie = $_POST['cookie'];
//执行SQL语句
$query = @mysql_query("select username, userflag from users "
."where username = '$username' and passcode = '$passcode'")
or die("SQL语句执行失败");
//判断用户是否存在,密码是否正确
if($row = mysql_fetch_array($query))
{
if($row['userflag'] == 1 or $row['userflag'] == 0) //判断用户权限信息是否有效
{
switch($cookie) //根据用户的选择设置cookie保存时间
{
case 0: //保存Cookie为浏览器进程
setcookie("username", $row['username']);
break;
case 1: //保存1天
setcookie("username", $row['username'], time()+24*60*60);
break;
case 2: //保存30天
setcookie("username", $row['username'], time()+30*24*60*60);
break;
case 3: //保存365天
setcookie("username", $row['username'], time()+365*24*60*60);
break;
}
header("location: main.php"); //自动跳转到main.php
}
else
{
echo "用户权限信息不正确";
}
}
else
{
echo "用户名或密码错误";
}
?>


复制代码 代码如下:


session_start();
if(isset($_COOKIE['username']))
{
@mysql_connect("localhost", "root","1981427") //选择数据库之前需要先连接数据库服务器
or die("数据库服务器连接失败");
@mysql_select_db("test") //选择数据库mydb
or die("数据库不存在或不可用");
//获取Session
$username = $_COOKIE['username'];
//执行SQL语句获得userflag的值
$query = @mysql_query("select userflag from users "
."where username = '$username'")
or die("SQL语句执行失败");
$row = mysql_fetch_array($query);
//获得用户权限信息
$flag = $row['userflag'];
//根据userflag的值输出不同的欢迎信息
if($flag == 1)
echo "欢迎管理员".$_SESSION['username']."登录系统";
if($flag == 0)
echo "欢迎用户".$_SESSION['username']."登录系统";
echo "注销";
}
else
{
echo "您没有权限访问本页面";
}
?>


复制代码 代码如下:


setcookie("username");
echo "注销成功";
?>

以上就介绍了cookies必须启用才能登入 php cookie 登录验证示例代码,包括了cookies必须启用才能登入方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template