2014-03-19 51 views
1

我PHP和MySQL的初學者,我想分享一個劇本,我想從這裏另一個腳本訪問定義PHP數組是一個場景如何分享在PHP腳本陣列

script1.php

<?php 
echo '<html> 
     <head></head> 
     <body> 
     <form action="script2.php" method="post" > 
     ....... 
     ....... some input 
     ....... 
     <button >Submit</button> 
     </form> 
     </body> 
     </html>'; 
?> 

script2.php

<?php 
........... 
........... 
some validation 
$age=array("Peter"=>"21","Ben"=>"45","Joe"=>"33"); 
some variable 
$x = 'Peter'; $y= 'Ben'; 

// click on button run script3. 
echo " <a href='script3.php?x=$x&y=$y'> 
<button>Submit</button></a>"; 
?> 

script3.php

<?php 

// Though I tried both require_once and include, I can't access array 'age' in script2 
require_once('script2.php'); 
include('script2.php'); 

// Here I am getting all variable defined in button part in actual code, but can't access array 
post_r($_GET); 

// Here I want to access array age in script2 
?> 

請注意,這不是實際的腳本,但這是我面臨的問題,我有索引文件,在第二個腳本中完成提交驗證後,表單被填充一次,如果驗證成功,則會立即顯示按鈕當我點擊按鈕腳本3將激活,在這裏我能夠接收在腳本中定義的變量,但我無法訪問在腳本2中定義的數組,雖然我一起嘗試以及單獨的這些函數require_onceinclude。如何共享數組?如何共享數組?請有人請給我你的一點時間,以便我可以學到一些東西......

+0

查找PHP會話 – Steve

回答

1

腳本1

session_start(); 
$_SESSION['key'] = insert_your_array; 

腳本2

session_start(); 
your_data = $_SESSION['key']; 

就是這樣。

Closing session

一旦使用完$_SESSION array,你會關閉會話。但是如果你不關閉它,那麼它將在default expiration timebrowser closes之後關閉。即使你也可以自定義session time out

功能關閉PHP會議上, session_close()

+1

+ 1非常感謝您的幫助 – user3304642

+0

我應該關閉會議嗎? – user3304642

1

您可以使用sessions訪問下一頁中的數組,或使用echo http_build_query($age);將其添加到鏈接的URL或您可以把在一個文件中的陣列和include它需要時在不同的文件中。

+0

+1非常感謝你的幫助,你的回答也工作,但我接受了他,因爲他首先回答非常感謝你再次 – user3304642