Page 1 of 1

Global Variables Question from a newbie :(

Posted: Sat Oct 06, 2007 2:18 pm
by lazersam
Hi all,

Please excuse my ignorance. I am not very good with PHP and only seldom practice but I have a problem. I understand PHP has been upgraded and this has led to some problems with my scripts. I used to globalise variables using session_register() but now that doesn't work !! I have researched and tested but can't find an alternative method. Even the $_session[] isn't working on my scripts. I just need to transfer variables across scripts.

Here I have two scripts as follows...

page1.php

Code: Select all

<?php

				session_start ();
				session_register(test);
				
                                                                $test = "hello";

				echo "<p><a href=\"page2.php\">Go to Page 2</a></p>";
				
				
				
				
?>
This would pass the variable contents to page2.php....

Code: Select all

<?php

				session_start ();
				
				
				echo "test=$test <br>";
				
				echo "<p><a href=\"page1.php\">Go to Page 1</a></p>";
				
				
?>
But now it doesn't work. Can someone tell me how to pass the content of $test please :)))??

Larry.

Posted: Sat Oct 06, 2007 2:27 pm
by RobertGonzalez

Code: Select all

<?php
session_start();
$_SESSION['session_var'] = 'This is some session var';
?>
I have also posted (within my last 10 posts I'd say) a brief sessions test codebase. You may want to look for that to get a decent way of starting a session, adding session vars, clearing session vars and ending a session.

Posted: Sat Oct 06, 2007 2:48 pm
by lazersam
Everah, I have amended the pages with $_session but the data does not 'stick'. Am I doing something wrong? On page1.php the output is empty and not passed to page2.php ??

page1.php

Code: Select all

<?php

				session_start ();
				$_SESSION['session_var'] = 'This is some session var'; 
				
				echo "<p><a href=\"page2.php\">Go to Page 2</a></p>";
				
				echo "session_var=$session_var <br>";
				
				
?>
outputs: session_var=

page2.php

Code: Select all

<?php

				session_start ();
				
				
				echo "session_var=$session_var <br>";
				
				echo "<p><a href=\"page1.php\">Go to Page 1</a></p>";
				
				
?>
Nothing is passed.

Posted: Sat Oct 06, 2007 2:55 pm
by Zoxive

Code: Select all

print $_SESSION['session_var'];
You need to use the GLOBAL Session array.

Posted: Sat Oct 06, 2007 2:59 pm
by lazersam
Zoxive wrote:

Code: Select all

print $_SESSION['session_var'];
You need to use the GLOBAL Session array.
Thanks Man, NOOOW I get it lol !! Thanks to Everah too :)

Larry.

Posted: Sun Oct 07, 2007 4:04 pm
by RobertGonzalez
On a side note, what you were doing before would have worked if register_globals was on. It is better for your that it is not on though, so make sure to always use the superglobal arrays $_GET, $_POST, $_COOKIE, $_SESSION, $_FILES, etc.