How to create a global variable and use it in other pages? - php

I tried this :
in page1.php :
$GLOBALS['nb_ajout_client'] = "rrrr";
in page2.php :
$GLOBALS['nb_ajout_client'] .= " kkkkk";
I displayed the value of $GLOBALS['nb_ajout_client'] but it is kkkkk.
So how to create a global variable and use it anywhere ?

Global variables only survive the lifetime of the program execution.
Each time you load a (PHP) page, you run a program from scratch. If you load a different page then you run a different program.
If you want to store data between them then you need to actually store it somewhere and then read it back from there in the other program.
If you want to do this globally, then the usual approach is to use a database.
If you want to do this on a per-user basis, then the usual approach is to use a session.
You could also pass the data via the browser (e.g. by putting it in the query string of a link and then reading it back from $_GET).

A variable is only 'global' in the current script. If you want to use a variable from page1, you need to include it from page2.

To pass variables to other page (other request) use PHP Sessions

you can try this.
g1.php
<?php
$GLOBALS['nb_ajout_client'] = "rrrr";
g2.php
<?php
include('g1.php');
$GLOBALS['nb_ajout_client'] .= " kkkkk";
var_dump($GLOBALS['nb_ajout_client']);

$GLOBALS are global in all scopes throughout a script.
To pass the value to another page:
use $_POST
use Session (server-side )
use cookies(client-side)

Related

Making a variable global for different requests

So, I have the following php variable in header:
<?php
$something = get_something();
?>
I have a page that is loaded via ajax and I want to use this variable without requesting it again.
How do I make this variable global so that I can use it even when a page is loaded via ajax?
Thanks
Your purpose will be served by session variables actually.
session_start();
$_SESSION['something'] = get_something();
Note: You need to call the session_start() function before setting / getting values if you haven't done it already for the same request.

PHP: Store PHP variable in page to use in POST

Currently I put a constant on my webpage using PHP which I then send, with Ajax, to my POST function. However, this leaves it susceptible to hacking (the user could change the variable with Firebug), so is there a way to store the variable in the PHP of the page and then access it later on, in the POST method (or is the GET variable of the page still available in the POST function, since that's where I get the variable from)?
I think what you have wanted is to store the post value to use it later.
Here you would need to use $_SESSION
You can do it like
session_start();
// Save variables into session
$_SESSION['thevalue'] = $_POST['value'];
If you wish to store between successive calls from the same user use the follwing:
<?php
session_start();
$_SESSION["your variable/constant"] = yourvaule;
Now use the variable as needed, accessing it as $_SESSION["your variable/constant"]
Hope it helps, and it's what you're asking.

How to access a variable from one PHP page to another, without a form, link, or button?

TLDR:- What is a good way to pass contents of a variable from one PHP file to another without involving a form, link or a button.
Question:-
So there is a form in a page/file called question_edit_form.php and its action attribute is already set to another file called question.php. The variable of interest is being read-in from the user in question_edit_form.php and is then obviously being sent to question.php using $_POST.
Now, there is a third file, named renderer.php, and which is not linked to the other two files. I want to use that variable of interest in this file. So how can I access that variable which is set in question.php from inside renderer.php?
first file -
session_start();
$_SESSION['your_variable'] = 'value';
other file -
session_start();
$var = $_SESSION['your_variable'];
this may help.
It sounds like you are using Moodle, in which case renderer.php is not an independent file; it contains the class definition for the renderer object used by question.php.
So... there is no need to pass the parameter between the scripts. If you really must access the form value directly from the renderer, just use the standard methods from the Moodle framework: required_param($name, $type) or optional_param($name, $default, $type).
Generally there are two methods available for you to pass on the value
Cookies
Sessions
How to use cookies:-
setcookie(name, value, expire);
e.g.
setcookie("user", "Alex Porter", time()+3600);
Access it using echo $_COOKIE['user'];
Second is Sessions. Here is how to use sessions:-
session_start();
$_SESSION['varname']=value;
Accessing page:-
session_start();
echo $_SESSION['varname'];
Additional info if required:-
Make sure you use session_start() at top of your page if not you may face with an headers already sent error / warning which again can be fixed by output buffering ob_start()
You can store the variables in the session.
http://www.w3schools.com/php/php_sessions.asp

Accessing session variables in a php file using javascript?

I want to know if there is a way to access the variables set by php using java-script, so that on one page the php variables are set. And then on the next page, i can use java-script to interrogate the PHP file in order to extract the variables, so that they can be displayed on another page?
Thanks in advance!
Not sure if this works, works for my get and post variables
var mySessionVariable = "<?php echo $_SESSION['sessionVariable']; ?>";
The only way, you would do it, is by setting cookies from PHP (or Javascript), and access these.. You can access cookies via PHP using $_COOKIE['var'], and via Js by, document.cookie("var")

How to pass variables from one php page to another without form?

I want to know how to pass a variable from one page to another in PHP without any form.
What I want to achieve is this:
The user clicks on a link
A variable is passed which contains a string.
The variable can be accessed on the other page so that I can run mysql queries using that variable.
use the get method in the url. If you want to pass over a variable called 'phone' as 0001112222:
<a href='whatever.php?phone=0001112222'>click</a>
then on the next page (whatever.php) you can access this var via:
$_GET['phone']
You want sessions if you have data you want to have the data held for longer than one page.
$_GET for just one page.
<a href='page.php?var=data'>Data link</a>
on page.php
<?php
echo $_GET['var'];
?>
will output: data
You can pass via GET. So if you want to pass the value foobar from PageA.php to PageB.php, call it as PageB.php?value=foobar.
In PageB.php, you can access it this way:
$value = $_GET['value'];
check to make sure the variable is set. Then clean it before using it:
isset($_GET['var'])?$var=mysql_escape_string($_GET['var']):$var='SomeDefaualtValue';
Otherwise, assign it a default value ($var='' is fine) to avoid the error you mentioned.
You can use Ajax calls or $_GET["String"]; Method
If you are trying to access the variable from another PHP file directly, you can include that file with include() or include_once(), giving you access to that variable. Note that this will include the entire first file in the second file.

Categories