Global Variables Question from a newbie :(

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Global Variables Question from a newbie :(

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Post 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.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Code: Select all

print $_SESSION['session_var'];
You need to use the GLOBAL Session array.
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
Post Reply