Global Variable in php and Javascript [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
passing variable between pages
I need to create a variable that I can use in one PHP page to assign a category to this variable. When I've assigned a value, how I can pass that value to another PHP page?
Do I have to use a global variable?

You can pass variables from page to page using:
Sessions.
A common config file that is included throughout all of your pages.
Forms (hidden fields etc).
Cookies.
Each approach has its own use, so you'll need to choose according to the circumstance. If the variable in question will stay the same throughout your app, I'd suggest placing it in a config file. If its a user-specific variable, I'd suggest using sessions.

Its depend on you what you want to use for passing value from one php page to another.
You can use Session, Cookie or Forms by using $_GET and $_POST method:
Example : By using Session
//page1.php
session_start();
$_SESSION['name'] = $name;
//you can access this in page2.php
session_start();
echo $_SESSION['name'];
Example : By using Cookies
//page1.php
setcookie(name, value, expire, path, domain);
//you can access it in page2.php like this
echo $_COOKIE["name"];
Example : By using GET and POST
You can use GET with anchor tag and for POST you need form submission.
Read more about GET and POST :
http://php.net/manual/en/reserved.variables.post.php
http://php.net/manual/en/reserved.variables.get.php
Hope this information will help you

Related

GET parameter PHP [duplicate]

This question already has answers here:
How do I pass data between pages in PHP?
(5 answers)
Closed 2 years ago.
I have HTML/PHP website (HTML code inside my .php)
I am using the $_GET["name"] method so that I can write the name of my guest in the link I send
(for exemple : mywebsite.com/index.php?name=Mike). Here's the code I use :
<p class="guest_info">
<?php echo " " . $_GET["name"]; ?>
<img alt="account_avatar" src="./index_files/avatar.svg">
</p>
Now, I would like this name to be implemented to the differents pages. My current problem is that if I give the link and after click on another page of my website, the ["name"] is not saved. I Would like to save the name used in the first given URL while navigating, and if I change the name in the link, that it automaticaly changes the pages aswell.
Thanks for helping
You should use a PHP session or a browser cookie.
Using the session route, at the beginning of your page, be sure to add session_start().
Then you can store the value as a session value.
if(array_key_exists('name', $_GET) && !array_key_exists('name', $_SESSION)) {
$_SESSION['name'] = $_GET['name'];
}
Now, anywhere after that code, you can use $_SESSION['name'] to access the value as long as the session persists.
In order for $_GET['name'] to persist from page to page, you need to keep passing it in the URL query string. For example, something like this: Link

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

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)

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

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