Cookie设置但未在其他页面显示php

时间:2019-06-29 10:27:00

标签: php cookies missing-cookies

我登录时有一个登录页面,过程页面设置了一个cookie,我想使用该cookie下次自动登录。 Cookie是在浏览器上设置的,我可以看到它,但是当我想在登录页面上使用它时,cookie为空(浏览器仍然有它,我可以看到它)

浏览器:mozilla

目录:

index.php (login page) --> in root /
include-function --> /functions/include-function.php
form-process.php --> /functions/form-process.php

我打开了php.ini的输出缓冲

注意:此帖子在回答后进行了编辑

这是我的登录页面:

<?php
require("./functions/include-function.php");
 ?>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="./style/form.css">
<script type="text/javascript"src="./js/form.js">

</script>
<title></title>
</head>
<body>
<div class="wrapper">
<div class="container">
    <h1>Welcome</h1>

    <form class="form" method="get" action="./functions/form-process.php">
        <input type="text" placeholder="Username" name="username">
    <input type="text" placeholder="Username" name="username" value="<?php print_r($_COOKIE); ?>">

        <input type="password" placeholder="Password" name="password">
        <button type="submit" id="login-button" name="submit" value="submit">Login</button>
    </form>
    </div>

    <ul class="bg-bubbles">
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    </ul>
    </div>
     </body>
     </html>

这是我的流程页面:

<?php require("./include-function.php"); ?>
<?php
if (isset($_GET['submit'])) {
$username = $_GET['username'];
$pass = $_GET['password'];
$message = validation_form($_GET);
$value = 45;
$cookieName = "us";
if ($username == "vahid@gmail.com" && $pass == "123456" && empty($message)) {
 $expired = time() + (60 * 60 * 24 * 1);
 setcookie($cookieName,$value,$expired);
 // redirect_to("http://www.youtube.com/");
 print_r($_COOKIE);
}else{
  redirect_to("../index.php");
}
}
?>

这是我的功能文件:

<?php
function redirect_to($location){
header("Location:". $location);
exit;
}

function  validation_form($value=[]){
$message = [];
// Field must not Empty
// trim() so empty space not count
$username =trim($value['username']);
$password =trim($value['password']);
if (!isset($username) || $username==="" || empty($username)
      || !isset($password) || $password==="" || empty($password)) {
      $message['validation'].="Username and Password must not empty";
}

// Length Check
$max = 20;
$min = 3;
if (strlen($username) > $max || strlen($password) > $max) {
  $message['validation'].= "Password Or Username Must not more than 20 character";
}
if (strlen($username) < $min || strlen($password) < $min) {
  $message['validation'].= "Password Or Username Must not less than 3 character";
}

// Type Check
if (!is_string($password)) {
  $message['validation'].= "Password Must be Number";
}

//Preg_match
if (!preg_match("/@/",$username)) {
  $message['validation'].= "Your username is your email ";
}
return $message;
}
?>

1 个答案:

答案 0 :(得分:0)

因此,如果我理解正确,那么您想在输入用户名和密码值的同时创建一个登录页面,下次刷新页面时您已经登录了吗?还是要制作粘性表格并保持之前插入的值?

顺便说一句,如果登录脚本不在操作表单脚本的同一文件夹中,则您不会在登录脚本中看到cookie,因为默认情况下,浏览器仅在设置该脚本的文件夹或子文件夹中的页面上发送cookie。 Cookie,在这种情况下为操作表单脚本。

因此,首先设置 $ path =“ /” 并将其作为setcookie参数, 告诉浏览器:“将Cookie发送到域或子域中的每个脚本”

另一点是,正如其他人在评论中已经告诉您的那样,setcookie会修改标头,并且所有标头更改必须必须在任何输出之前完成,并且在您的情况下,在设置之前输出空白Cookie。 如果需要先输出某些内容,则可以使用输出缓冲区的功能,其中可以将所有输出缓冲在标头之前,并仅在设置标头后发送。

进一步了解https://www.php.net/manual/en/book.outcontrol.php

虽然我认为您将此页面设为“ try'n”,但在实际使用情况下,请勿实现这种登录方式。 因为您正在以可读格式在http请求中传递用户名和密码。 使用 php会话数据库中的散列字段,以有效,更安全的方式存储客户端数据,
为了使用户更轻松地访问页面,请制作一个粘性表格,将字段值插入存储变量中的值,而不是打印存储它们的整个数组。

希望这会有所帮助!

相关问题