有没有办法只使用PHP将数组放入cookie?

时间:2017-10-26 01:35:51

标签: php arrays cookies

我从我的数据库中获取数据并尝试使用mysqli_fetch_assoc($ result)将它们放入cookie中。

最后,setcookie功能不起作用。 这是我的代码:

<?php
if(!isset($_POST['remember']) && !isset($_COOKIE['ProfileData']))
{
    session_start();
}
else if(isset($_POST['remember']))
{
    $remember = $_POST['remember'];
}

if (!isset($_SESSION['ProfileData']) && !isset($_COOKIE['ProfileData']))
{
    $username = $_POST['username'];
    $password = $_POST['password'];
}
$birthday = "DATE_FORMAT(Birthday, '%d/%m/%Y')";

if (!isset($username) && !isset($_SESSION['ProfileData']) && !isset($_COOKIE['ProfileData']))
{
    header("location: /login.php");
}
else if(isset($username))
{
    $con = mysqli_connect('localhost','root','root','Signum','3306');   
    $sql = "SELECT Id, Name, Surname, $birthday, Genre, Username FROM MyTable WHERE Username = '$username' and Password = '$password'";
    $result = mysqli_query($con,$sql);

    if(isset($remember))
    {
        setcookie("ProfileData", mysqli_fetch_assoc($result), time()+(60*60*24*365));
    }
    else
    { 
        $_SESSION['ProfileData'] = mysqli_fetch_assoc($result); 
    }
}
?>

1 个答案:

答案 0 :(得分:-1)

您可以使用serialize函数对其进行序列化。这是更改的行代码:

getChildState

从cookie获取此值时,您需要使用unserialize函数再次转换为数组。