I would like to store information in a golbal php variable that can be accessed by a new web page when a link is clicked.
I am developing a web site that will have 3 different types of users. On my home page, I have three navigational links, that, when clicked takes the member to a specific home page for that type of user. From this page, the user can log in, or register as a new user. If they are registering as a new user, the link opens a registration form which is used for all three types. I would like to be able to use a php function to store the user type in a global varibale that will then be used by a php function to set the type of user when creating the mySql record.
Thanks
Global Variables
Moderator: General Moderators
Re: Global Variables
Did you read the PHP.net page about globals? It doesn't sound like it.
Re: Global Variables
I have the been researching this for the better part of 4 hours - incluing php.net, but have not been able to find anything that addresses my question.
I have been playing around with different versions of the code below to see if I can set the variables in the volCalc.php script file, and then call the calcVol() function from a new page opened by a link in the first.
volCalc.php file
<?php
$X = 0;
$Y = 0;
$Z = 0;
function CalcVol(){
global $x;
global $y;
global $z;
$vol = $x * $y * $z;
return $vol;
}
?>
Initial web page
<?php include("volcalc.php"); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<?php
function setValues() { //Sets the global variables in the included script file
$X=20;
$Y=30;
$Z=5;
}
?>
<a href="vcalc2.php" onclick="setValues()">Test Next Page</a>
</body>
</html>
Linked Web Page
<?php include("volcalc.php"); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<b>Calculated Volume: </b>
<?php
echo calcVol(); // external function containing the globals variables
?>
</body>
</html>
calcvol() returns 0.
If I set the variables and call the function all within the same php script from with in the Initial web page, I get the correct answer.
How do I store the values for X, Y & Z for use in functions called by suceeding web pages? I suscect has something to do with the life of the external script file. Is it destroyed when I leave the first page and go to the second, where it is intialized agian, leaving the values of the variables = 0? If so, is there a way to keep a script file active so other pages can gain access to the variables and use those variables in functions?
I do appreciate your help.
I have been playing around with different versions of the code below to see if I can set the variables in the volCalc.php script file, and then call the calcVol() function from a new page opened by a link in the first.
volCalc.php file
<?php
$X = 0;
$Y = 0;
$Z = 0;
function CalcVol(){
global $x;
global $y;
global $z;
$vol = $x * $y * $z;
return $vol;
}
?>
Initial web page
<?php include("volcalc.php"); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<?php
function setValues() { //Sets the global variables in the included script file
$X=20;
$Y=30;
$Z=5;
}
?>
<a href="vcalc2.php" onclick="setValues()">Test Next Page</a>
</body>
</html>
Linked Web Page
<?php include("volcalc.php"); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<b>Calculated Volume: </b>
<?php
echo calcVol(); // external function containing the globals variables
?>
</body>
</html>
calcvol() returns 0.
If I set the variables and call the function all within the same php script from with in the Initial web page, I get the correct answer.
How do I store the values for X, Y & Z for use in functions called by suceeding web pages? I suscect has something to do with the life of the external script file. Is it destroyed when I leave the first page and go to the second, where it is intialized agian, leaving the values of the variables = 0? If so, is there a way to keep a script file active so other pages can gain access to the variables and use those variables in functions?
I do appreciate your help.
Re: Global Variables
Hello,
My first post!!!
page1.php
page2.php
My first post!!!
page1.php
Code: Select all
session_start();
$_SESSION['X'] = 2;
$_SESSION['Y'] = 3;
$_SESSION['Z'] = 5;
Code: Select all
session_start();
$vol = $_SESSION['X'] * $_SESSION['Y'] * $_SESSION['Z'];