FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


How to make Global Variables with PHP?





wangbin218200
Hey, im kinda new to php, i have some experience programming with it, but i was wondering if there are such things as Global Variables with php, like the $session variables with ASP? a variable i can pass to another page to make something with it, but without the need to put it in the address bar like this : hxxp://www.something.com/login.php?var1=value&var2=value.
I want this two variables for example to be "global" so that i can use them in anothere php page.
Thanks.
Roald
You could use method post or you could use cookies or you could use session.
rohan2kool
hxxp://www.something.com/login.php?var1=value&var2=value

1. it should be http:// rather than hxxp://

2. You can refer to these variables like: $_GET['var1'] and $_GET['var2']. That's a much better way (read: secure) to do it rather than using global varialbes.

3. If you want to use ASP like global variables, first note that ASP sucks. Secondly, to do this you need to set the php.ini directive 'register_globals' as 'on'. Now, you can do this only and only if your server is under total control of urself. Most hosting services don't allow you to edit the php.ini file, so you have to use $_GET[], $_POST[], $_COOKIE[] and $_REQUEST[] arrays.
Philip
sure there is

Code:
session_start();
if (!isset($_SESSION['login'])) {
  $_SESSION['login'] = true;
}


this will make the $_session['login'] alway have true value in all pages, Smile and as long you dont close the pages.
snowboardalliance
TO use variables across pages you either use $_SESSION[] and session_start() (or cookies), $_GET[] and the ?foo=bar&var=12 part on the URL in links, or you can use a form with hidden input fields that save input between pages <input type="hidden" name="$variable" value="$value" />. It depends on what you are trying to do and what your preference is. Like the form method doesn't make sense if there is no form, but the $_GET[] method requires ALL links to have the variables, and they can easily be changed removed by users.
rohan2kool
oops.. sorry didn't get the question in the first place. my bad. and as other's have said it already, you can use $_SESSION[] for that.
ammonkc
rohan2kool wrote:

Secondly, to do this you need to set the php.ini directive 'register_globals' as 'on'.


correct me if I'm wrong, but turning on 'register_globals' in your php.ini file does make variables "global" in the sense that the variables will hold their values accross different pages. That is what $_SESSION[] and $_COOKIE variables are for.
turning on register_globals makes the type of varible ($, $_GET, $_POST, etc) irrelevant. for example:
<?php
// if you have $_POST['username'] from a form on the previous page
// you could retrieve the username with $_GET['username'] or $username
echo $username;// will output the value in $_POST['username'] passed from the form
?>
rohan2kool
ammonkc wrote:

correct me if I'm wrong, but turning on 'register_globals' in your php.ini file does make variables "global" in the sense that the variables will hold their values accross different pages. That is what $_SESSION[] and $_COOKIE variables are for.
turning on register_globals makes the type of varible ($, $_GET, $_POST, etc) irrelevant. for example:
<?php
// if you have $_POST['username'] from a form on the previous page
// you could retrieve the username with $_GET['username'] or $username
echo $username;// will output the value in $_POST['username'] passed from the form
?>


nope you aren't wrong. but i thought that was precisely what he wanted to do rather than keep variables across pages.. i said it.. my bad Very Happy
ammonkc
Yeah, I had to go back and read it a second time to make sure, because thats what I thought also at first.
Related topics
php variables
extremely short SESSION lifetime
php error ?[parse error]
PHP Problem...
phpNuke Register new users Problem Problem
Hot Or Not script not working
destroy\unset sessions?
Help with Dreamweaver sessions !!!!!!!!
help with pagination problem
Simple php & mysql script
PHP Security
Global variables in jommla 1.0.x?
Global variables
Form handling in php is a pain in the neck
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.