[英]I am unable to make a session in my php file in localhost. I am redirected even though i created a session
This is my code for login.html 这是我的login.html代码
<html>
<head>
<title>User Logon</title>
</head>
<body>
<h2>User Login </h2>
<form name="login" method="post" action="login.php">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
Remember Me: <input type="checkbox" name="rememberme" value="1"><br>
<input type="submit" name="submit" value="Login!">
</form>
</body>
</html>
This is login.php 这是login.php
<?php
/* These are our valid username and passwords */
$user = 'jonny4';
$pass = 'delafoo';
if (isset($_POST['username']) && isset($_POST['password']) ){
if (($_POST['username'] == $user) && ($_POST['password'] == $pass)) {
if (isset($_POST['rememberme'])) {
/* Set cookie to last 1 year */
setcookie('username', $_POST['username'], time()+60*60*24*365);
setcookie('password', md5($_POST['password']), time()+60*60*24*365);
} else {
/* Cookie expires in an hour */
setcookie('username', $_POST['username'],time()+60*60);
setcookie('password', md5($_POST['password']),time()+60*60););
}
header('Location: index.php');
} else {
echo 'Username/Password Invalid';
}
} else {
echo 'You must supply a username and password.';
}
?>
and this is index.php 这是index.php
<?php
/* These are our valid username and passwords */
$user = 'jonny4';
$pass = 'delafoo';
if (isset($_COOKIE['username']) && isset($_COOKIE['password'])) {
if (($_POST['username'] != $user) || ($_POST['password'] != md5($pass))) {
header('Location: login.html');
} else {
echo 'Welcome back ' . $_COOKIE['username'];
}
} else {
header('Location: login.html');
}
?>
Now,index files checks for cookie and if unavailable, redirects to login.html 现在,索引文件会检查Cookie,如果不可用,则重定向到login.html
login.html takes values, passes to login.php which creates cookies. login.html会获取值,并传递给login.php来创建cookie。
But no matter how many times i do it, the output is same, i am not able to view index file because there is no cookie.. any help? 但是无论我做多少次,输出都是一样的,因为没有cookie,所以我无法查看索引文件。
You need to change this section: 您需要更改此部分:
if (($_POST['username'] != $user) || ($_POST['password'] != md5($pass))) {
header('Location: login.html');
} else {
echo 'Welcome back ' . $_COOKIE['username'];
}
To: 至:
if (($_COOKIE['username'] != $user) || ($_COOKIE['password'] != md5($pass))) {
header('Location: login.html');
} else {
echo 'Welcome back ' . $_COOKIE['username'];
}
Note that $_POST
changed to $_COOKIE
请注意,
$_POST
更改为$_COOKIE
Check your index.php file. 检查您的index.php文件。 It has error
它有错误
<?php
/* These are our valid username and passwords */
$user = 'user';
$pass = 'user';
if (isset($_COOKIE['username']) && isset($_COOKIE['password'])) {
//var_dump($_COOKIE['username']); exit;
if (($_COOKIE['username'] != $user) || ($_COOKIE['password'] != md5($pass))) {
header('Location: login.html');
} else {
echo 'Welcome back ' . $_COOKIE['username'];
}
} else {
header('Location: login.html');
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.