a0101 Posted June 23, 2012 Share Posted June 23, 2012 Hi, so for example I created a variable in webpage A, $x = array(A,B,C) . And I want use the values of the array in webpage B, is there any way to make the variable 'global' so that when I print_r $x in webpage B, it will read the values that I stored in webpage A? Quote Link to comment https://forums.phpfreaks.com/topic/264665-same-variable-for-multiple-webpages/ Share on other sites More sharing options...
ZulfadlyAshBurn Posted June 23, 2012 Share Posted June 23, 2012 If they uses the same domain and server, you can use session. Or else you have to use PHP GET to transfer the details. Quote Link to comment https://forums.phpfreaks.com/topic/264665-same-variable-for-multiple-webpages/#findComment-1356442 Share on other sites More sharing options...
a0101 Posted June 23, 2012 Author Share Posted June 23, 2012 Thanks! Didn't know about the sessions function before. However, I would like to echo a value of an array, through its position within the array. So what I've done is basically: Webpage A session_start(); $_SESSION['fruits'] = $fruits; $fruits = array('apple', 'banana', 'pear'); Webpage B session_start(); echo $_SESSION['fruits'][3]; It doesn't give me the value of pear but a string offset error, so how should I go about doing this? Quote Link to comment https://forums.phpfreaks.com/topic/264665-same-variable-for-multiple-webpages/#findComment-1356456 Share on other sites More sharing options...
ZulfadlyAshBurn Posted June 23, 2012 Share Posted June 23, 2012 You have to store the value in the variable before saving it in the session. use this session_start(); $fruits = array('apple', 'banana', 'pear'); $_SESSION['fruits'] = $fruits; session_start(); echo $_SESSION['fruits'][3]; before you try out the script, clear your session first using. create a new php file and run it <?php session_destroy(); ?> this will ensure it destroys all the previous session used. Quote Link to comment https://forums.phpfreaks.com/topic/264665-same-variable-for-multiple-webpages/#findComment-1356458 Share on other sites More sharing options...
a0101 Posted June 23, 2012 Author Share Posted June 23, 2012 Works perfectly fine now, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/264665-same-variable-for-multiple-webpages/#findComment-1356462 Share on other sites More sharing options...
ZulfadlyAshBurn Posted June 23, 2012 Share Posted June 23, 2012 No problem Quote Link to comment https://forums.phpfreaks.com/topic/264665-same-variable-for-multiple-webpages/#findComment-1356463 Share on other sites More sharing options...
Pikachu2000 Posted June 23, 2012 Share Posted June 23, 2012 You could also define the array in a separate file, and include() it in scripts that need access to those values. Quote Link to comment https://forums.phpfreaks.com/topic/264665-same-variable-for-multiple-webpages/#findComment-1356466 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.