variable linked in page URL, how-to and are spaces okay? - php

3 quick questions:
I want to create one page "view_build.php" which, when opened, takes a variable from the URL contained within it, and displays unique information for that 'build'. For example
$row[0]
The above is a link to a page called view_build.php?buildname=VARIABLE
Does the .php file contain the variable in a $_GET array, JUST from me including a variable name in the html link?
And if so, is it okay if there are spaces in it? For example:
view_build.php?buildname=Dual Zoren
Lastly, how do I include more than one variable in the URL? What is the syntax?
Thanks so much!
Regards.

Does the .php file contain the variable in a $_GET array, JUST from me including a variable name in the html link?
Yes. To elaborate, if you call view_build.php via an HTTP request with the URL query parameters foo=bar, then within that script, you can access $_GET['foo'] which will have the value bar.
is it okay if there are spaces in it?
No, they should be URL encoded. See http://php.net/manual/function.urlencode.php
For example
<?= htmlspecialchars($row[0]) ?>
Lastly, how do I include more than one variable in the URL? What is the syntax?
URL query parameters are separated by the & character, eg
view_build.php?buildname=Dual+Zoren&foo=1&bar=2

Related

Grab all text after variable

I'm a little unsure of how to word this one but essentially, I want to achieve the following:
http://my.website/?url=http://another.website/?var1=data&var2=moredata&id=119
And for the URL variable to be: http://another.website/?var1=data&var2=moredata&id=119
Naturally, PHP sees var2 and id as new variables. This would be used to pass a full URL from one page to another, however, it poses an issue when the page already has its own variables in the URL!
Any help appreciated!
You need to encode the secondary url which you're putting inside the url variable when you create it. This will ensure it doesn't contain special querystring characters that the receiving website will misunderstand. If the code in my.website is PHP too then the urlencode function (http://php.net/manual/en/function.urlencode.php) is your friend. For example:
urlencode("http://another.website/?var1=data&var2=moredata&id=119")
produces
http%3A%2F%2Fanother.website%2F%3Fvar1%3Ddata%26var2%3Dmoredata%26id%3D119
which will not be misunderstood by the PHP code reading it as containing further separate variables.

Redirecting by Header in php

I am using wampserver for php and mysql database.
in my php file, after executing some code to insert in mysql database, I want to redirect browser to another html page that has php code in it. This page needs value of the variable id to fetch data from my sql database. So I send value of id by below code in the end of php code.
header('Location: lostItem.php?id=$id');
The recieving file has below code to get value of id.
$id= $_GET['id'];
But, it turns out that there is no value of id passed i.e. the receiving file shows url :
http://localhost/Lost%20and%20Found/lostItem.php?id=$id
instead of showing
http://localhost/Lost%20and%20Found/lostItem.php?id=11
I already have another page that sends same data (value of id) to receiving file. It has below code in it for that.
echo "<a class='listOfItems' href='lostItem.php?id=$id'>";
echo $item;
echo "</a>";
And that works fine. But when I try to do same thing by using header, it doesn't work. Before using header, I have made sure that variable $id has right integer value. But it doesn't send that value by using header.
Is it that data can't be sent by this method using header? If so, please suggest an alternative method.
basic php notation, in single quotes variables are not interpreted
header("Location: lostItem.php?id=$id");
to be strict, location should use a full URI not a relative one
header("Location: http://www.example.com/lostItem.php?id=$id");
use curly bracket upon variable when you used variable value in string, Please try this
header("Location: lostItem.php?id={$id}");
Single quotes does not process variables, PHP ignores all your variables so you need to concatenate variable using concatenate operator (.)
header('Location: lostItem.php?id='.$id); // fast than double quotes
or
header("Location: lostItem.php?id=$id");

Pass a URL in a GET for PHP

I need to pass a url using a GET address. To give an example which was I have tried:
http://www.example.com/area/#http://www.example.com/area2/
I've also tried replacing the forward slashes with other characters but that doesn't seem to work. How would you pass a url in a GET?
As I have understood, you should use url_encode() and url_decode().
The function url_encode() lets you create a string that can be used as a link.
You should use it this way:
$link = 'goto.php?link=' . url_encode($_POST['target_site']);
And when you were going to redirect to the user defined site (eg), you can decode the parameter given this way:
$decoded_link = url_decode($_GET['link']);
// Now it's safe to use the given URL (for example I can redirect to there)
header('location: ' . $decoded_link);
Hope it helps.
The # character links to an anchor on the page. The browser will automatically scroll to the element with the id after the point sign. So that's not what you're looking for.
To pass a GET parameter, the syntax would be like this:
http://example.com/area?http://example.com/area2
Then, if you var_dump($_GET), you'll see your URL. But, if you have other fields you also want to pass in your URL, you can use key=value pairs, like so:
http://example.com/area?url=http://example.com/area2&param1=a&param2=b
In this case, your URL will be available in $_GET['url'].

$_GET variable in php without using a form

i have gallery.php?gid=1 in gallery.php page and i have to submit data to save.php without using a form.
and i want to get the value of gid without using a button..
is that possible?
gallery.php
$gid=$_GET['gid'];
save.php
echo $gid..
header("location:save.php?gid=".$gid);
OR
use a anchor tag with href="save.php?gid=<?php echo $gid;?>"
$_GET will contain all the variables which are in the URL, after the "?" (foo.php?gid=...).
You are not obliged to use a form to fill these variables.
The members of $_GET array is got from query string. In a simple way, a query string is tha part after the ? character of the URL and formatted in this way:
param1=value1&param2=value2&....
So you can pass any pair of key-value in to your PHP script by adding a query string to the end of this PHP file URL.

ignore '&' in var contents passed in address bar

i have a download.php file which gets and opens files. i have a problem is that files were named using '&' in the file name so i get file not found when trying to access files with '&' in them.
example: download.phpf=one_&_another.pdf
in the download.php file i use get to the the file name ($_GET['f']) the example above throws the error file not found if i change the file name to one_and_another.pdf it works.
Yes renaming would be nice if there wasnt a whole lot of these files named this way.
I need to know how to ignore the fact that '&' doesnt mean im about to pass another var in php.
If you can control the query strings, you need to URL encode the ampersands so they look like this:
download.php?f=one_%26_another.pdf
Then look for $_GET['f'] as usual. Otherwise a literal ampersand & would break $_GET into
{ 'f' => 'one_', '_another.pdf' => '' }
You will probably just need to urlencode() the & properly in your links:
download.php?f=one_%26_another.pdf
Rule number 1 for accepting user input: do not trust it.
Refer to this StackOverflow answer for your solution.

Categories