简体   繁体   English

我没有序列化

[英]I dont get serializing

Okay, so im making a (very) basic register system which takes form $_POST data , makes $_SESSION data of it and then i need to make it so that it fwrites to a file, but i need to be able to get the sessions out later so i can use them when someone loggs in. 好的,即时通讯正在制作一个(非常)基本的寄存器系统,该系统采用$ _POST数据形式,使它成为$ _SESSION数据,然后我需要将其写成文件,但是我需要能够获取会话稍后退出,以便有人登录时可以使用它们。
A friend of mine told me i should serialize it but i don't know how to do so. 我的一个朋友告诉我我应该序列化它,但是我不知道该怎么做。 Also i am very new to programming and stackoverflow. 我也是编程和stackoverflow的新手。

My code : 我的代码:

<?php session_start();

$_SESSION["naam"]            = $_POST["naam"];
$_SESSION["email"]           = $_POST["email"];
$_SESSION["woonplaats"]      = $_POST["woonplaats"];
$_SESSION["telefoon"]        = $_POST["telefoonnummer"];
$_SESSION["gebruikersnaam"]  = $_POST["gebruikersnaam"];
$_SESSION["wachtwoord"]      = $_POST["wachtwoord"];
$filename                        = "inloggegevens.txt";




$error = 0;


if (empty($_SESSION["naam"])) {
    echo "<pre>U moet een naam invullen\n</pre>";
    $error++;
    } else {
    $_SESSION["naam"];
    }


if (empty($_SESSION["email"])) {
    echo "<pre>U moet een email-adres invullen\n</pre>";
    $error++;
    } else {
    $_SESSION["email"];
    }


if (empty($_SESSION["woonplaats"])) {
    echo "<pre>U moet een woonplaats invullen\n</pre>";
    $error++;
    } else {
    $_SESSION["woonplaats"];
    }


if (empty($_SESSION["gebruikersnaam"])) {
    echo "<pre>U moet een gebruikersnaam invullen\n</pre>";
    $error++;
    } else {
    $_SESSION["gebruikersnaam"];
    }


if (empty($_SESSION["wachtwoord"])) {
    echo "<pre>U moet een wachtwoord invullen\n</pre>";
    $error++;
    } else {
    $_SESSION["wachtwoord"];
    }


if ($error == 0){

    if (is_writable($filename)) {
        echo "Fwrite succes";
        $fd = fopen($filename, "r+");
        fwrite($fd,$_SESSION["naam"]);
        fwrite($fd,$_SESSION["email"]);
        fwrite($fd,$_SESSION["woonplaats"]);
        fwrite($fd,$_SESSION["telefoonnummer"]);
        fwrite($fd,$_SESSION["gebruikersnaam"]);
        fwrite($fd,$_SESSION["wachtwoord."]);
        fclose($fd); 
    } else {
    echo "non de sjon.";
    } 
 }  
?>

When you serialze a string or an array you will always have the same format: 当您序列化字符串或数组时,您将始终具有相同的格式:

type:value and seperated with semicolon. 类型:值,以分号分隔。

Assumed you want serialize the "naam" and the "email": 假设您要序列化“ naam”和“ email”:

$serializeArray = array(
    'naam' => $_SESSION["naam"],
    'email' => $_SESSION["email"]
);

$serialized = serialize($serializeArray);

Output: 输出:

print_r($serialized);
a:2:{s:4:"naam";s:3:"Max";s:5:"email";s:11:"test@ggg.de";}

as you can see your array with naam and email is now a very long string. 如您所见,包含naam和email的数组现在是一个很长的字符串。 "a" stands for "array", the 2 behind "a" is the number of elements in your array. “ a”代表“数组”,“ a”后面的2是数组中元素的数量。 Inside the serialized array you have some strings (indicated with the small "s".) After this there is the number of characters like "4". 在序列化数组内部,您有一些字符串(用小写的“ s”表示。)之后有许多字符,例如“ 4”。 This counts the letters of "naam". 这算出“ naam”的字母。 After this there is the content of the string. 之后是字符串的内容。 Like "naam". 像“ naam”。 Then semicolon for next element, and so on ... 然后是下一个元素的分号,依此类推...

Now Store $serialized in your file. 现在将$ serialized存储在文件中。 After this, get the stored serialized String from your file and save it in $serialized. 之后,从文件中获取存储的序列化String并将其保存在$ serialized中。 Then unserialize it: 然后反序列化它:

$unSerialized = unserialize($serialized);

Output: 输出:

print_r($unSerialized);
Array ( [naam] => Max [email] => test@ggg.de )

Summary: 摘要:

serialize( mixed value ) converts your value (array or object or string or whatever) to a very long string with informations of all of your values within your serialized value. serialize( Mixed value )将您的值(数组或对象或字符串或任何东西)转换为一个很长的字符串,其中包含序列化值内所有值的信息。 After you unserialze your serialized string you get your original condition of your value. 解串序列化的字符串后,您将获得原始的条件值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM