问题
在我登录后,我的会话确实存在。这很好(但即使在关闭浏览器后仍保留登录页面)。但在我进入下一页后登录时,会话没有该页面存在。 问题
未在表格中插入任何项目,因为第二页上不存在会话。 请查看代码以便清楚理解。
这是admin_login.php
<?php
if(isset($_SESSION["manager"])){
echo "hello boss,go to<a href='inventory_list.php'>inventory Page</a>";
}
else{
if(isset($_POST["user"]) && isset($_POST["pass"])) {
$manager=preg_replace('#[^A-Za-z0-9]#i','',$_POST["user"]);
$pass=preg_replace('#[^A-Za-z0-9]#','',$_POST["pass"]);
}
require_once "connect_to_db.php";
$sqlcommand='SELECT id FROM admin WHERE username="$manager" AND password="$pass" LIMIT 1';
$query=mysqli_query($connect_dude,$sqlcommand);
$numrow=mysqli_num_rows($query);
if($numrow==0){
header("location:admin_login.php");
}
$row=mysqli_fetch_assoc($query);
$id=$row['id'];
$manager=$row['username'];
$pass=$row['password'];
$_SESSION['manager']=$manager;
$_SESSION['password']=$pass;
$_SESSION["id"]=$id;
echo "go to <a href='inventory_list.php'>inventory page</a>";
exit();
}
session_destroy();
?>
<html>
<head>
<title>admin login page</title>
</head>
<body>
<form id="formfield" name="formfield" method="post" action="admin_login.php" >
User Name:
</br><input type="text" name="user" id="user" />
</br>
Password:
</br>
<input type="password" name="pass" id="pass" />
</br>
<input type="submit" id="subm" value="login" />
</form>
</body>
</html>
这是inventory_list.php的代码
<?php
session_start();
if(isset($_SESSION["manager"])){
$id=preg_replace('#[^0-9]#i','',$_SESSION["id"]);
$manager=preg_replace('#[^A-Za-z0-9]#i','',$_SESSION["manager"]);
$password=preg_replace('#[^A-Za-z0-9]#i','',$_SESSION["password"]);
include "connect_to_db.php";
$sqlcommand="SELECT COUNT(id) FROM admin WHERE id='$id' AND username='$manager' AND password='$password' LIMIT 1";
$counting=mysqli_query($connect_dude,$sqlcommand);
$numrow=mysqli_num_rows($counting);
if($numrow==0){
echo "you better get out of here";
exit();
}
if(isset($_POST["product_name"])){
$productname=preg_replace('#[^A-Za-z]#i','',$_POST["product_name"]);
$productprice=preg_replace('#[^0-9]#i','',$_POST["product_price"]);
$productdetails=preg_replace('#[^A-Za-z0-9]#i','',$_POST["product_detail"]);
$productdivision=$_POST["product_division"];
$productsubdivision=$_POST["product_subdivision"];
include "connect_to_db.php";
$sql="SELECT COUNT(id) FROM products WHERE product_name='$productname' ";
$counting=mysqli_query($connect_dude,$sql) ;
$numrow=mysqli_num_rows($counting);
if($numrow>0){
echo "you have added the product before";
print_r($numrow);
}
$sql="INSERT INTO products (product_name,price,details,category,subcategory,date_added) VALUES ($productname,$productprice,$productdetails,$productdivision,$productsubdivision,now())";
mysqli_query($connect_dude,$sql) ;
$pid=mysqli_insert_id();
$newfile=$pid.jpg;
move_uploaded_file($_FILES['image']['tmp_name'],"$productsubdivision/$newfile");
header("location: inventory_list.php");
exit();
}
}
else{
echo "hello Killer";
echo "go to <a href='admin_login.php'>login page</a>";
exit();
}
session_destroy();
?>
<html>
<head>
</head>
<body>
<fieldset>
<legend value="fucking add something">Add product,dude</legend>
<form id="formo" action="inventory_list.php" method="post" enctype="multipart/form-data">
Product Name:<input type="text" name="product_name" id="product_name" placeholder="Type Product Name" /></br>
Product price:<input type="text" name="product_price" id="product_price" placeholder="Type Product price" /></br>
Product division:
<select id="product_division" name="product_division">
<option>select</option>
<option value="food">food</option>
<option value="apparel">apparel</option>
<option value="beauty">beauty</option>
<option value="toys">toys</option>
</select>
</br>
Product type:
<select id="product_subdivision" name="product_subdivision" >
<option value="">select</option>
<option value="Rice">Rice</option>
<option value="milk">milk</option>
<option value="Meat">Meat</option>
<option value="sweet">sweet</option>
<option value="fruits">fruits</option>
<option value="cap">cap</option>
<option value="shirt">shirt</option>
<option value="pant">pant</option>
</select>
</br>
Product Detail:<textarea id="product_detail" name="product_detail" placeholder="Type Product Name" ></textarea>
</br>
product Image:<input type="file" id="image" name="image" value="upload Product pic" />
</br>
<input type="submit" name="button" id="button" value="Add This Item Now" />
</form>
</fieldset>
</body>
</html>
感谢您的时间。
答案 0 :(得分:1)
一种可能的解决方案一种可能的解决方案可能是启用浏览器cookie。如果cookie被禁用,则 $ _ SESSION [“manager”] 将无法在那里使用。
答案 1 :(得分:1)
在页面顶部添加以下行:
<?php
session_start() // Add this line
if(isset($_SESSION["manager"])){
从第1页删除session_destroy。它会奏效。凉爽...
答案 2 :(得分:0)
请在管理页面
下面的行下面添加开始会话<?php
session_start() // Add this line in your code. it will work fine.
if(isset($_SESSION["manager"])){
echo "hello boss,go to<a href='inventory_list.php'>inventory Page</a>";
如果此答案解决了您的问题,请将其标记为答案,以便其他人可以在将来使用