怀疑PHP中的代码相关会话 [英] doubt in code related sessions in php

查看:54
本文介绍了怀疑PHP中的代码相关会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在php coding.i中,Iam abeginner在arraysession.php中有12到19行的疑问。当表单被提交时,$ POST [''form_products]会被定义。然后$ _SESSION [''products''是不是它是空的但条件不满意,但在书中他们给出的条件是满意的。请你澄清如何。为什么他们使用序列化和unserialize.i已采取这个代码为sams教你自己的PHP .please帮我理解这段代码





arraysession.php

 1:<?php  
2:session_start();
3:?>
4:< !DOCTYPE html >
5:< html >
6:< head >
7:< title < span class =code-keyword>> 使用会话存储数组< / title >
8:< / head >
9:< body >
10:< h1 > 产品选择页< / 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'] =序列化($ products);
18:} else {
19:$ _SESSION ['products'] = serialize($ _ POST ['form_products']);
20:}
21: echo < p>您的商品已注册!< / p>;
22:}
23:?>
24:< 表格 方法 = 发布 action = <?php echo $ _SERVER ['PHP_SELF']; ??> >
25:< p > < 标签 = form_products > 选择一些产品:< / label > < ; br / < span class =code-keyword>>
26:< select id = form_products 名称 = form_products [] 多个 = multiple

size = 3 >
27:< span class =code-keyword>< 选项 value = Sonic Sc​​rewdriver > 声波螺丝刀< / option >
28:< 选项 value = Hal 2000 > Hal 2000 < / option >
29:< 选项 value = Tardis > Tardis < / option >
30:< span class =code-keyword><
选项 value = ORAC > ORAC < / option >
31:< 选项 value = 运输车手链 > 运输车手链< < span class =code-leadattribute> / option >
32:< / select > < / p >
33:< ; 按钮 type < span class =code-keyword> = 提交 名称 = 提交 value = 选择 > 提交表单< / button >
34:< / form >
35: < p > < a href = session1.php > 转到内容页< / a < span class =code-keyword>> < / p >
36:< / body >
37:< / html >



session6.php



 1:<?php  
2:session_start();
3:?>
4:< !DOCTYPE html >
5:< html >
6:< head >
7:< title < span class =code-keyword>> 访问会话变量< / title >
8:< / head >
9:< body >
10:< h1 > 内容页面< / h1 >
11:<? php
12: if (isset($ _ SESSION ['products'])){
13: echo < strong>您的购物车:< / strong>< ol>;
14: foreach (反序列化($ _ SESSION ['products]) as $ p){
15: echo < li> 。$ p。 < / li> ;;
16:}
17:echo
< / ol > ;
18:}
19:?>
20:< p>< a href =
arraysession.php >返回产品选择页< / a>< / p>



注意

www.it-



添加的代码块[/ Edit ]

解决方案

检查POST [''form_products]它被定义了。然后


_SESSION [''products'']是否为空,但条件不满意,但在书中他们给出的条件是满意的。请你澄清我怎么。为什么他们使用序列化和unserialize.i已经采取这个代码为sams教你自己的PHP。请帮助我理解这段代码





arraysession.php

 1:<?php  
2 :session_start();
3:?>
4:< !DOCTYPE html >
5:< html >
6:< head >
7:< title < span class =code-keyword>> 使用会话存储数组< / title >
8:< / head >
9:< body >
10:< h1 > 产品选择页< / h1 >
11:<? php
12: if (isset(


_POST ['form_products'])){
13: if (!empty(


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

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

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]

解决方案

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

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(


这篇关于怀疑PHP中的代码相关会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆