Click here to Skip to main content
15,914,419 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Iam abeginner in php coding.i have doubt in 12 to 19 lines in arraysession.php.when form is submited,$POST['form_products] is checked wheter it is defined.then $_SESSION['products']is cheked whether it is empty but condition not satisgfied,but in the book they gave condition is satisfied.can u please clarify me how .why they have used serialize and unserialize.i have taken this code for "sams teach ur self php".please help me to understand this code


arraysession.php
PHP
1: <?php
2: session_start();
3: ?>
4: <!DOCTYPE html>
5: <html>
6: <head>
7: <title>Storing an array with a session</title>
8: </head>
9: <body>
10: <h1>Product Choice Page</h1>
11: <?php
12: if (isset($_POST[‘form_products’])) {
13: if (!empty($_SESSION[‘products’])) {
14: $products = array_unique(
15: array_merge(unserialize($_SESSION[‘products’]),
16: $_POST[‘form_products’]));
17: $_SESSION[‘products’] = serialize($products);
18: } else {
19: $_SESSION[‘products’] = serialize($_POST[‘form_products’]);
20: }
21: echo "<p>Your products have been registered!</p>";
22: }
23: ?>
24: <form method="post" action="<?php echo $_SERVER[‘PHP_SELF’]; ??>">
25: <p><label for="form_products">Select some products:</label><br />
26: <select id="form_products" name="form_products[]" multiple="multiple"
size="3">
27: <option value="Sonic Screwdriver">Sonic Screwdriver</option>
28: <option value="Hal 2000">Hal 2000</option>
29: <option value="Tardis">Tardis</option>
30: <option value="ORAC">ORAC</option>
31: <option value="Transporter bracelet">Transporter bracelet</option>
32: </select></p>
33: <button type="submit" name="submit" value="choose">Submit Form</button>
34: </form>
35: <p><a href=""session1.php"">go to content page</a></p>
36: </body>
37: </html>

session6.php

PHP
1: <?php
2: session_start();
3: ?>
4: <!DOCTYPE html>
5: <html>
6: <head>
7: <title>Accessing session variables</title>
8: </head>
9: <body>
10: <h1>Content Page</h1>
11: <?php
12: if (isset($_SESSION[‘products’])) {
13: echo "<strong>Your cart:</strong><ol>";
14: foreach (unserialize($_SESSION[‘products]) as $p) {
15: echo "<li>".$p."</li>;
16: }
17: echo "</ol>";
18: }
19: ?>
20: <p><a href=""arraysession.php"">return to product choice page</a></p>

NOTE
www.it-

[Edit]Code blocks added[/Edit]
Posted
Updated 23-Feb-13 0:15am
v2

1 solution

I have removed the extra "?" from the below code at line 24:
PHP
<?php echo $_SERVER[‘PHP_SELF’]; ???> should be <?php echo $_SERVER[‘PHP_SELF’]; ??>

Now, the code seems to be ok.

In PHP, serialize() function is used to generates a storable representation of a value.
http://php.net/manual/en/function.serialize.php[^]

In PHP, unserialize() function is used to create a PHP value from a stored representation
http://php.net/manual/en/function.unserialize.php[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900