2013-02-24 33 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