我需要创建一个包含会话和cookie的登录表单。我在登录时尝试了代码以及注销,但cookie对我不起作用。我是一个友好的帮助,下面是我的登录和注销代码。 Thanx提前 的login.php:
<?php
session_start();
$message="";
if(count($_POST)>0) {
$conn = mysqli_connect('localhost','root','Spiegel@123','sachin') or
die('Unable To connect');
$result = mysqli_query($conn,"SELECT * FROM login WHERE name='" .
$_POST["name"] . "' and password = '". $_POST["password"]."'");
$row = mysqli_fetch_array($result);
if(is_array($row)) {
$_SESSION["id"] = $row[id];
$_SESSION["name"] = $row[name];
} else {
$message = "Invalid Username or Password!";
}
}
if(isset($_SESSION["id"])) {
setcookie("name", $_SESSION["name"], time()+60*60*24*100, "/");
header("Location:one.php");
}
?>
<html>
<head>
<title>User Login</title>
</head>
<body>
<form name="frmUser" id="frmUser" method="post" action="" align="center">
<div class="message"><?php if($message!="") { echo $message; } ?></div>
<h3 align="center">Enter Login Details</h3>
Username:<br>
<input type="text" name="name">
<br>
Password:<br>
<input type="password" name="password">
<br><br>
<input type="submit" name="submit" value="Submit">
<input type="reset">
</form>
</body>
</html>
Logout.php:
<?php
session_start();
if(session_destroy()) // Destroying All Sessions
{
unset($_COOKIE['name']);
header("Location: login.php"); // Redirecting To Home Page
}
?>
答案 0 :(得分:0)
你需要在之前的时间设置cookie,如下所示:
setcookie('name', null, -1, '/');