简体   繁体   中英

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.
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.

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":

$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. "a" stands for "array", the 2 behind "a" is the number of elements in your array. Inside the serialized array you have some strings (indicated with the small "s".) After this there is the number of characters like "4". This counts the letters of "naam". After this there is the content of the string. Like "naam". Then semicolon for next element, and so on ...

Now Store $serialized in your file. After this, get the stored serialized String from your file and save it in $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. After you unserialze your serialized string you get your original condition of your value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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