Php 使用cookies存储数据,如何使用cookies防止用户未登录就进入网页
如果用户尚未登录,但单击“订单”页面,则应将其重定向到“登录”页面。然而,它并没有像预期的那样发挥作用。它只保留在登录页面中,无法登录或进入订单页面。问题出在哪里 订购表格::Php 使用cookies存储数据,如何使用cookies防止用户未登录就进入网页,php,session,login,Php,Session,Login,如果用户尚未登录,但单击“订单”页面,则应将其重定向到“登录”页面。然而,它并没有像预期的那样发挥作用。它只保留在登录页面中,无法登录或进入订单页面。问题出在哪里 订购表格:: // Connects to your Database $connect = mysql_connect("127.0.0.1","root","") or die("not connecting"); mysql_select_db("shop") or die("no db :'("); // is the o
// Connects to your Database
$connect = mysql_connect("127.0.0.1","root","") or die("not connecting");
mysql_select_db("shop") or die("no db :'(");
// is the one accessing this page logged in or not?
if ( !isset($_SESSION['logged-in']) || $_SESSION['logged-in'] !== true)
{
// not logged in, move to login page
header('Location: member_login.php');
exit();
}
else
{
//count the number of food
$food_num = mysql_query("select count(*) as sum from food");
$ttl_num= mysql_fetch_array($food_num);
//select the price of each food item
for($i=1; $i<=$ttl_num['sum']; $i++){
$price_query = mysql_query("SELECT price FROM food where foodid = '$i'");
$price_array= mysql_fetch_array($price_query);
$price_food[$i]=$price_array['price'];
//select the name of each food item
$name_query = mysql_query("SELECT name FROM food where foodid = '$i'");
$name_array= mysql_fetch_array($name_query);
$name_food[$i]=$name_array['name'];
mysql_close($connect);
}
}
?>
//连接到您的数据库
$connect=mysql_connect(“127.0.0.1”,“根”,“根”)或die(“未连接”);
mysql_选择数据库(“商店”)或死亡(“无数据库:”(“”);
//访问此页面的人是否已登录?
如果(!isset($_会话['logged-in'])|$_会话['logged-in']!==true)
{
//未登录,移动到登录页面
标题('Location:member_login.php');
退出();
}
其他的
{
//数一数食物的数量
$food_num=mysql_查询(“选择count(*)作为来自food的总和”);
$ttl\u num=mysql\u fetch\u数组($food\u num);
//选择每种食物的价格
对于($i=1;$i)
登录检查:
<?php
$connect = mysql_connect("127.0.0.1","root","") or die("not connecting");
mysql_select_db("shop") or die("no db :'(");
$form = $_POST['submit'];
$email = $_POST['loginID_member'];
$password = $_POST['password_member'];
if( isset($form) )
{
if( isset($email) && isset($password) && $email !== '' && $password !== '' )
{
$sql = mysql_query("SELECT * FROM member WHERE memberemail='$email' and memberpw='$password';");
if( mysql_num_rows($sql) != 0 ) { //success
$_SESSION['logged-in'] = true;
header('Location: order_form.php');
exit;
}
else
{
echo "<script>alert('Incorrect email or password!');window.location.href='member_login.php';</script>";
}
}
else
{
die('<script type="text/javascript">alert("Please enter a username and password!");location.replace("member_login.php")</script>');
}
}
mysql_close($connect);
?>
这里我有几点建议:
- 开始时只需使用会话,因此您根本不需要
$\u COOKIE
(如果您希望会话在浏览器会话之间保持不变,可以稍后添加此选项)
- 在访问
$\u SESSION
之前,您需要调用,让PHP知道您想要会话功能
- 因此,您的第一张支票应该是:
if (isset($SESSION['logged'])) {
header("order_form.php");
exit();
}
- 现有的
if
子句中有两个错误:您将重定向到的文件拼写错误,并且没有使用大括号,因此将始终执行退出。对于控制块(循环和条件),您应该始终使用大括号(一些样式指南鼓励使用单行无括号if
块,我认为这是一种不好的做法)
我建议您解决这些问题,并解决SQL注入安全问题,然后继续。下一步将是调试,即在会话中设置一些内容,并确保在下一个请求时会话阵列中可以使用这些内容
如果你能设置一个版本控制系统(比如Git),事情会变得容易得多因此,您可以定期快照您的工作。然后,如果出现问题,您可以回滚到最后一个已知的点。哦,天哪,这里有一些SQL注入漏洞。在所有进入查询的用户数据都未被篡改之前,请不要将其激活。您正在使用,并且应该使用。您还容易受到现代API的攻击从中更轻松。啊,每次使用header('Location:x')
时,您都应该使用exit()
。header函数只向客户端发送一个header,但在此之后,您的PHP脚本将很高兴地继续执行代码,这不是您想要的。那么我该如何更改?如果它将代码放在SQL之前,那么在我添加“exit()”后,它也无法工作'在这两个代码和测试的末尾,它返回了错误的组合。..wt是问题所在吗?您的header()
调用是否在您期望的时候运行?