2013-02-24 31 views
1

我想将一些数据存储到会话中,以便在页面之间进行传输。我将有很多图像,需要一种有效的数据存储方式。所以我做了将会话存储在href中

<a href="create2.php?" ><?php?$_SESSION["choice_1"]=2;?><img src="pictures/laptops/reebok.png" height="150"></td></a>;

然而,当我打电话的另一页的会议,它说,这是不确定的。我可以这样做,或者单击图像时,我可以将一些数据存储到变量中,调用函数,将变量保存到会话中,然后加载新页面。但是我不知道我该怎么做。任何帮助将不胜感激谢谢

+1

你的意思是你想为每个图像存储一个会话变量,或者你想为点击的图像存储一个会话变量?如果你想要第二个,你应该使用Ajax,或者改用Javascript和cookies。 – mavrosxristoforos 2013-02-24 22:35:24

回答

2

月1日,你的HTML被打破,有一个</td>是不应该存在。

关于你的问题,你不必在URL中传递值(但是不是这样做的),如果你不想使用cookies,只需要会话ID,然后用它来开始会话。 您的链接:之前的一切

<a href="create2.php?<?php echo session_name().'='.session_id(); ?>"> 
    <img src="pictures/laptops/reebok.png" alt="reebok" height="150"> 
</a> 

在create2.php通话session_start()。当然,如果您使用cookie,则不需要在URL中传递会话标识。

+0

我已经完成了' dizazta 2013-02-24 23:23:46

+0

Why are you writing '$_SESSION["choice_1"]' and '"2"' there? Copy paste the html I wrote, in the calling php make sure you put '$_SESSION["choice_1"] = 2;' and in create2.php you should be able to do 'echo $_SESSION["choice_1"];'. Of course you need 'session_start();' in both pages. To simplify that, just use cookies and forget about passing the session id in the URL. – Eggplant 2013-02-24 23:26:28

+0

Maybe you don't really want to use a SESSION variable, but just to pass a value via URL? Try this: 'reebok'。在create2.php中使用'<?php if(isset($ _GET ['choice_1']))echo $ _GET ['choice_1']; ?>' – Eggplant 2013-02-24 23:47:56

1

请确保您在每个PHP文件的顶部打电话session_start(),然后再尝试访问/修改会话变量。

+1

将'anything'写入文档之前,包括空格和换行符。 – Marty 2013-02-24 22:33:50

0

首先,你的php init标签是错的<?php?应该是<?php。其次,你应该在你的PHP来初始化会话:

<?php 
// nothing before here, session_start must be the very first thing on the page 
session_start(); 

最后,你不创造任何PHP的链接,这样,你只定义变量,但它无关温特的a标签。

要发送的存储在SESSION全局变量到达网址,你应该做的“choice_1”:

<a href="create2.php?choice_1=<?=$_SESSION['choice_1']?>"> 
    <img src="pictures/laptops/reebok.png" height="150"> 
</a>; 
+0

你可以在其他一些PHP代码之后调用'session_start()',你不能在文档上预先输出任何东西。 – Marty 2013-02-24 22:38:04