How can I pass the value of variable $login to the page try.php ? I wish to use this code:
<a href="try.php">Vote</a> ?
How can I pass the value of variable $login to the page try.php ? I wish to use this code:
<a href="try.php">Vote</a> ?
There are two main ways to pass data via a URL. You can create a <form>
that will then POST the data to the next page. Alternatively you can pass data directly in the url, which is called the GET method.
The POST method means that the data will be passed without the user being able to easily see it. The data is sent in the HTTP headers to the next page. This is the most common and recommended way to pass form data across.
The GET method passes all the data in the URL like so: http://www.somesite.com/page.php?var1=stuff&var2=morestuff
which means you can even craft a URL to pass data via GET yourself. You just need to urlencode() the data before you put it into the URL so that it won't contain any invalid characters that will cause the URL to be invalid.
Just to be throrough you can also use a session to pass data between pages. But in order to do this you must first establish the session using session_start(). This method is more useful for keeping data persistent across page loads than passing user input to a new page. You'll still need to use POST or GET to pass information that a a user types into the first page to the second page to process it and then you can store it in a $_SESSION variable to keep make it persist across all of the next page loads without having to keep passing it via GET or POST.
Let's say your site is example.com, and you wanted to send whether or not the user has logged in yet to a page called Login.php. You could set href to example.com/login.php?login=true
. This will create a variable called $_GET['login']
and set its value to whatever you put after the equal sign (in this example, 'true'). You can access this 'GET' variable from your PHP script, though you first need to make sure it is set, and has a value (isn't empty). You can do this with the following:
if (isset($_GET['login']) && !empty($_GET['login']))
$login = $_GET['login'];
Let's say the information you are sending is dynamic, and so needs to be set based on a variable in your PHP script. You could do this:
<a href="try.php?login=<? echo $login; ?>">Vote</a>
You want to be careful with what information you send using GET variables though. This is because since they are sent through the URL, a user can set it himself. For example, set's say Alice logs in to her account, which she has her private information on. For every page Alice visits, the URL has a ?login=Alice
at the end of it. But Bob wants to find out Alice's information, so he looks at his URL, which is example.com/Login.php?login=Bob
and changes 'Bob' to 'Alice,' so he has example.com/Login.php?login=Alice
. Now, he can look at all of Alice's information. Because the user can set the URL, and all 'GET' variables through it, you need to be careful to only send 'GET' information that won't cause security issues if changed. If you need to send information like this, you can use 'POST' variables instead.
You can use $_SESSION, i don't know what you are trying to do.. But it seems that is what you need!
If $login is just a string or other basic variable, use a query string.
<a href="try.php?myvar=<?=$login?>">Vote</a>
There are a few ways of passing values to a PHP page.
To pass a value as a GET request you add a parameter as follows:
try.php?myvalue=test
It can then be retrieved on the destination script:
$myValue = isset($_GET['myvalue'])?$_GET['myvalue']:null;
This needs to be escaped to avoid SQL injections. See How can I prevent SQL injection in PHP?.
However, using GET is highly discouraged if your variable contains sensitive information such as a password. For that it is better to use a POST request. You should explain what $login
is being used for.