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
Related
I can pass two variables page to page via,..
In HTML:
Pass 5 and 9
In PHP:
echo " Pass 5 and 9 ";
And retrieve them with $_GET
BUT the data is visible to the user
How can I do this so the data is not visible to the user?
I'm assuming I should use POST but really need some sample code.
Thanks for any help.
Sessions might be what you are looking for.
//then at any point you can set a session variable
$_SESSION['new_var'] = $_POST['new'];
This question already has answers here:
URL rewriting with PHP
(5 answers)
Closed 7 years ago.
Perhaps I'm using the wrong search terms to try to find this online, but I am trying to accomplish the task of passing a variable in a URL path, with using an identifier.
For example, here is my current URL: http://www.myurl.com/test/index.php?name=bob
On my index.php page, I would set something along the lines of $name = $_GET['name']; and have no issue using this variable.
My goal, however, would be to use the URL: http://www.myurl/test/bob/ and still be able to receive "bob" as the name variable in my script.
Is this possible, hypothetically? Thank you!
put in your htaccess a mod_rewrite statement like
RewriteRule ^test\/([a-z]+)\/?$ index.php?name=$1
One of the easiest ways to do this (and also a bit more secure) is instead of using a GET statement, using a session variable. You could change the url to be whatever you like using mod_rewrite as you have suggested, however you can still access the variable without anything special.
For instance, you just start your session like so
session_start();
then set your session variable, like so (assuming you have already defined $name):
$_SESSION['name'] = $name;
and then on the page where you'd want to get name, put session_start(); at the top of the page, and then, instead of $_GET['name'] just call the variable as $_SESSION['name'] instead.
This way you don't really need to worry about using the URL for passing the variable from one page to another. It won't be affected by rewriting.
Of course, your other option, if you wish to continue using it as a GET variable is this: https://stackoverflow.com/a/8228851/3044080
This question already has answers here:
CSRF (Cross-site request forgery) attack example and prevention in PHP
(4 answers)
Closed 9 years ago.
I have a form.php wich action call sql.php file like:
SQL.PHP
if ($_REQUEST['action'] == "add") {
}
if ($_REQUEST['action'] == "edit") {
}
I'm like to prevent direct access, because user can call from browser url: http://sql.php?action=add
One way is check if a submit. Seem work well.
if( isset($_POST['Submit']) && ($_POST['Submit'] == "Submit") )
{
echo "direct access not allowed";
}
There is better alternatives?
Use the $_SERVER['HTTP_REFERER'] array to detect if someone is accessing your page directly by typing in it into the browser, or comming from another one of your pages, by a use of links from a page.
So, basically.
if($_SERVER['HTTP_REFERER'] == 'about.php'){
//let user do something
}
So, the $_SERVER['HTTP_REFERER'] global stores information of the pages you visit, and if you place echo that code, in your page, it will tell you from which page your are comming from. meaning, that if you only typed the page and access it, it will give 0/false value.
So, you can use it to detect if someone is directly typing the page or comming from one of your pages.
As others have indicated already, using tokens, and sessions would be a better idea since this method can be manipulated. So, I recommend you google them out
It should be if(!isset($_POST['Submit']) . Also if you use method="POST", it does not throw your parameters like ?action=add at the browser. method="GET" does it.
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
My question is pertaining to extracting information from the URL using PHP.
I've three pages in the website I am building
catalogues.html
form_userdata.php
mail.php
The schema is as follows. The user will choose a file to download from catalogues.html
for eg
a href="form_userdata.php?id=1026&name=Vibrating Feeder - Heavy duty -VFH" target="_blank"
This id & name will be passed to form_userdata.php and after entering the details in form_userdata.php page, the page passes control to mail.php, that will check if the fields are all true and valid.
My question is how can I use the ID & name specified in "a href" in my code?
I am passing the ID and name from catalogues->form_userdata and collecting it in mail.php
Thank you for your valuable input
you can use sessions to store it when processing form_userdata.php and then use it in mail.php. That way you don't have to pass it in every URL.
when I add $id=$_POST['id']; at
form_userdata, I get an error saying
"undefined index: id"
That's quite logical, since you're passing them in the URI, and thus have to read them using $_GET in PHP, ie. do :
$id = isset($_GET['id']) ? (int)$_GET['id'] : null;
I found this solution:
session_start();
$_SESSION['id'] = $id=$_GET['id'];
$_SESSION['name'] = $name=$_GET['name'];
this will pass the value of ID and Name to the next page.
Thank you for your help
you may use $_GET to collect and pass variables
You can access the named values in an URL (query string, query-info part) by using the $_GET superglobal. It will contain the value by it's name:
$_GET['id']
More information is in the the PHP manual: $_GET. That page is already pretty specific, I suggest you read this manual page as well: Variables From External Sources it more broadly describes how this works.
As input is very important in programming, it's really recommended to understand how it works so you can use the language for your needs more easily.