php将会话更改为cookie

时间:2016-04-29 10:08:15

标签: php session cookies

感谢您的阅读。我已经成功制作了注册并登录的php文件。但是,我需要将SESSION改为COOKIE。将SESSION替换为COOKIE似乎并没有起作用。如何将SESSION更改为COOKIE并使该功能与SESSION完全相同?

Register.php:

include 'config.php';

if(isset($_SESSION['username'])!="")
{
  header("location: welcome.php");
}

if(isset($_POST['submit']))
{
    $sql = "INSERT INTO user (username, password) VALUES ('".$_POST["username"]."','".$_POST["password"]."')";
    if (mysqli_query($con, $sql)) 
    {
    $_SESSION['username'] = $_POST["username"];
    header("location: login.php");
    } 
    else
    {
      echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }
}

?>

login.php:

<?php
include("config.php");
session_start();

if(isset($_SESSION['username'])!="")
{
  header("location: welcome.php");
}

if (mysqli_connect_errno())
{
   echo "MySQLi Connection was not established: " . mysqli_connect_error();
}

if(isset($_POST['login']))
{
   $username = mysqli_real_escape_string($con,$_POST['username']);
   $password = mysqli_real_escape_string($con,$_POST['password']);

   $sel_user = "SELECT * FROM user WHERE username='$username' AND password='$password'";
   $run_user = mysqli_query($con, $sel_user);

   $check_user = mysqli_num_rows($run_user);

   if($check_user>0){
      $_SESSION['username']=$username;
      $_SESSION['loggedIn'] = true;
      echo "<script>window.open('index.php','_self')</script>";
   }
   else {
      echo "<script>alert('username or password is not correct, try again!')</script>";
   }

}

?>

2 个答案:

答案 0 :(得分:0)

你有很多功能可以在PHP中使用Cookies,它们与会话有点不同。

设置coockie:

setcookie("Name_Of_Your_Cookie", "Value");

如果你想设置一个自动过期时间,你也可以使用第三个参数(例如,如果你在一周之后不想自动说明你的Coockie)

setcookie("Name_Of_Your_Cookie", "value", time()+60*60*24*7);

要检查您的coockie的价值,您可以像会话一样使用它,例如:

if(isset($_COOKIE['Name_Of_Your_Cookie'])) echo "Welcome ".$_COOKIE['Name_Of_Your_Cookie'];
else header('Location: ./login.php');

要取消设置Cookie(Deconnexion部分),您有两个建议:

unset($_COOKIE['Name_Of_Your_Cookie'];

或者设置过去的有效时间(cookie将被视为obsolet)

setcookie("Name_Of_Your_Cookie", "value", time()-1);

如果您想了解更多细节,可以在PHP官方网站上找到很好的文档,在这里: http://php.net/manual/fr/features.cookies.php

希望能帮到你

答案 1 :(得分:0)

试试这个

NB :将用户名,密码,密码等敏感凭据存储为Cookie是不合理的,SESSION is more secure than COOKIE..

  

<强> Register.php

<?php
include 'config.php';
if(isset($_COOKIE['AuthVal']))
       unset($_COOKIE['AuthVal']);

if(isset($_POST['submit']))
{
    $sql = "INSERT INTO user (username, password) VALUES ('".$_POST["username"]."','".$_POST["password"]."')";
    if (mysqli_query($con, $sql)) 
    {
    // COOKIE will be there in browser upto 30 days
    setcookie ('AuthVal', 'username='.$_POST['username'].'&loggedIn=true', time() + (3600 * 24 * 30));
    header("location: login.php");
    } 
    else
    {
      echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }
}
?>
  

<强>的login.php

<?php
include("config.php");

if(isset($_COOKIE['AuthVal']))
   parse_str($_COOKIE['AuthVal']);


if(isset($username) && $username != "" && isset($loggedIn) && $loggedIn == "true"){
   header("location: welcome.php");
   exit;
}

if (mysqli_connect_errno())
   echo "MySQLi Connection was not established: " . mysqli_connect_error();

if(isset($_COOKIE['AuthVal']))
       unset($_COOKIE['AuthVal']);

if(isset($_POST['login']))
{
   $username = mysqli_real_escape_string($con,$_POST['username']);
   $password = mysqli_real_escape_string($con,$_POST['password']);

   $sel_user = "SELECT * FROM user WHERE username='$username' AND password='$password'";
   $run_user = mysqli_query($con, $sel_user);

   $check_user = mysqli_num_rows($run_user);

   if($check_user>0){
      // COOKIE will be there in browser upto 30 days
      setcookie ('AuthVal', 'username='.$_POST['username'].'&loggedIn=true', time() + (3600 * 24 * 30));
      echo "<script>window.open('index.php','_self')</script>";
   }
   else {
      echo "<script>alert('username or password is not correct, try again!')</script>";
   }

}    
?>