php variables and url and specifically ?'s - php

I'm trying to write a script which accepts a url such as rfile.php?url=https://example.com/file.php?alias=midlands how ever I want to be able to pass vairables over so it becomes rfile.php?url=https://example.com/file.php?alias=midlands?type=src how ever $type=$_GET['type']; comes up as null.
I having checked $url=$_GET['url']; returns the full url string of rfile.php?url=https://example.com/file.php?alias=midlands?type=src
Clearly I can fix this but seperating the features I planned to use via the type vairable but I thought I would ask if any one knows a way to pull in that url including the ?alias=midlands but stop at ?type=src ?
Example url - rfile.php?url=https://example.com/file.php?alias=midlands?type=src
<?php
/*
*/
header("Pragma: No-Cache");
// Initialise variables
$url=NULL;
$type=NULL;
// Check auth is set
if (!isset($_GET['url']))
{
echo "Could not service your request. url was not set";
}
// Check auth is set
if (!isset($_GET['type']))
{
echo "Could not service your request. type was not set";
}
$url=$_GET['url'];
$type=$_GET['type'];
echo $url;
echo $type;
?>
Thanks
Terran

I'm pretty sure that what you want is to pass parameters and their values into your URL and grab them with $_GET method.
If this is what you are looking for, then you are having a typo in your example URL. The ? tells the URL where to start looking for parameters. Each pair of parameter-value must be separated with character &. This means that the correct format of your URL in order to retrieve all those parameters is this one:
rfile.php?url=https%3A%2F%2Fexample.com%2Ffile.php%3Falias%3Dmidlands&type=src
Since you want to have a url as a parameter of the original url you should try to urlencode the parameter as above.
Probably your results will be:
$_GET['url'] => https%3A%2F%2Fexample.com%2Ffile.php%3Falias%3Dmidlands
and
$_GET['type'] => src
Then u will have to run urldecode again on your $_GET['url'] to grab the nested parameters. Hope I've helped.

Related

How to use $_GET variable (index.php?cat=about)

I know there must already has this question been asked but I didn't find the answer cause I don't know how to search exactly what I want.
So, I wanna make such a link http://example.com/index.php?cat=about where I should insert some about info. But I don't know how to make a page with this URL containing ? symbol and other stuff, or how to edit that page then, where to edit and etc.
About Us
I've also made cat.php file but what next?
In your index.php file you can use the following:
if(isset($_GET['cat']) { // means if the user use the url with ?cat=something
echo "<1>About {$_GET['cat']}</h1>"; //print the about of the cat as html
}
OK, Suppose you've two PHP files. page_a.php & page_b.php.
page_a.php
<?php
echo "<a href='page_b.php?cat=about'>Click Me</a>";
page_b.php
<?php
print_r($_GET); // Show all GET contents
echo $_GET['cat']; // Show what content exist in 'cat' part of url
Hope, this will clear your doubt of how to send data in url from one page to another using GET mehtod.
To store variable data in the url, you can use the query string. This is the portion of the url that immediately follows the protocol, domain name and file path
The query string begins with ? and may contain one or more parameter parameter value pairs. The parameter and parameter value are separated by =. Each pair is separated by &.
Let's suppose you do development for a tourism company called Gi Tours which provides content in 3 different languages. Because site visitors will want to be able to select their preferred language, you can provide flag images that will indicate a certain language and wrap those flags with the appropriate hyperlinks. Rather than writing out the full language name, you can simply assign id numbers to represent each like this:
<?php
echo "<img src=\"img/ge.png\">";
echo "<img src=\"img/en.png\">";
echo "<img src=\"img/ru.png\">";
?>
If a visitor clicks the second flag which loads this url: https://www.gitours.ge/index.php?lang=2, your index.php code can be written to extract the value assigned to lang by using $_GET["lang"].
If you write in your index.php file:
<?php
echo $_GET["lang"];
?>
Your code will display:
2
Or on your index.php file, you can easily generate dynamic page content using $_GET array data.
<?php
if(isset($_GET["lang"])){ // this checks if lang exists as a parameter in the url
$lang=$_GET["lang"]){ // $lang will equal the value that follows lang=
}else{
$lang=1; // if there was no lang parameter, this sets the default value to 1
}
if($lang==2){
// show English content
}elseif($lang==3){
// show Russian content
}else{
// show Georgian content
}
?>
This is, of course, a simplified demonstration; other techniques can be used to interact with the $lang value.
#Lasha Palelashvili let suppose below example:
above you are sending only one input parameter cat through url to the index.php file at server side so when you send this data from url it will send by get method by default, if you want to get this url info(input parameter) at the php side so $_GET will help you to fetch that info $_GET is actually an array which stores your input parameter as key and input parameter's value as value for your case("index.php?cat=about") $_GET array will contain value like below:
$_GET = array("cat" => "about")
now at the server side you can easily get the value like:
//index.php
<?php
$cat = $_GET["cat"];
echo $cat;
?>

Getting values resulted from accessing the URL with query (php)

I have a URL like given below
http://mydomain.com/wrt/search.php?org=125&assignedto[]=NULL&Search=req_assigned_to&state[]=New&state[]=Pending%3A+Installation&state[]=Pending%3A+More+Info&state[]=Assigned&state[]=Working&orgs[]=125
i need to get the values of other required variables which results from accessing the above URL given in PHP..
Actually i should use this above mentioned link to access the form varaible values which will be displayed in that form resulted by execution of some queries using the parameters passed through the link given above..i wont be using form concepts such but can use this link alone,as i dont have access to the required database so this link is the base to retrieve values ...
Please suggest how it can be done.Its of high urgency.
You can access URL parameter via $_GET, when you have this URL try for example:
echo $_GET['Search']; should output req_assigned_to.
I'm not quite sure what you are trying to do but if I'm right you are looking for:
echo $_GET['org'];
This will echo 125. replace org with any other parameter in your url.
Read up on: http://www.php.net/manual/en/reserved.variables.get.php

Issue with & in a string submitted with $_GET

I'm building an "away"-page for my website and when a user posted a link to another website, each visitor clicking that link will be redirected first to the away.php file with an info that I am not responsible for the content of the linked website.
The code in away.php to fetch the incoming browser URI is:
$goto = $_GET['to'];
So far it works, however there's a logical issue with dynamic URIs, in example:
www.mydomain.com/away.php?to=http://example.com
is working, but dynamic URIs like
www.mydomain.com/away.php?to=http://www.youtube.com/watch?feature=fvwp&v=j1p0_R8ZLB0
aren't working since there is a & included in the linked domain, which will cause ending the $_GET['to'] string to early.
The $goto variable contains only the part until the first &:
echo $_GET['to'];
===> "http://www.youtube.com/watch?feature=fvwp"
I understand why, but looking for a solution since I haven't found it yet on the internet.
Try using urlencode:
$link = urlencode("http://www.youtube.com/watch?feature=fvwp&v=j1p0_R8ZLB0") ;
echo $link;
The function will convert url special symbols into appropriate symbols that can carry data.
It will look like this and may be appended to a get parameter:
http%3A%2F%2Fwww.youtube.com%2Fwatch%3Ffeature%3Dfvwp%26v%3Dj1p0_R8ZLB0
To get special characters back (for example to output the link) there is a function urldecode.
Also function htmlentities may be useful.
You can test with this:
$link = urlencode("http://www.youtube.com/watch?feature=fvwp&v=j1p0_R8ZLB0") ;
$redirect = "{$_SERVER['PHP_SELF']}?to={$link}" ;
if (!isset($_GET['to'])){
header("Location: $redirect") ;
} else {
echo $_GET['to'];
}
EDIT:
Ok, I have got a solution for your particular situation.
This solution will work only if:
Parameter to will be last in the query string.
if (preg_match("/to=(.+)/", $redirect, $parts)){ //We got a parameter TO
echo $parts[1]; //Get everything after TO
}
So, $parts[1] will be your link.

Get URL parameters having URL parameters in it

I want to read URL parameters from URL and I have used $_Get["parametername"]. And it works fine.
However, for URL like :
http://myserver.com/getcrawledimage.php?key=XXXXXXXXXX
&url=https://www.google.co.in/search? q=read+json+in+mysql&aq=f&oq=read+json+in+mysql&aqs=chrome.0.57j0l3j62l2.7010j0&sourceid=chrome&ie=UTF-8#hl=en&sclient=psy-ab&q=parsing+json+in+mysql&oq=parsing+json+in+mysql&gs_l=serp.3..0j0i22i30l3.138180.199873.3.200144.44.28.10.5.6.2.824.5388.1j22j1j1j1j0j2.28.0...0.0...1c.1.12.psy-ab.6dwtA9Be9BY&pbx=1&bav=on.2,or.r_qf.&bvm=bv.46340616,d.bmk&fp=cb8cca45920a2be6&biw=1920&bih=979&devicetype=retina.
it doesn't work.
I am tracking key, url and devicetype here.
It returns key=XXXXXXXXXX , url=https://www.google.co.in/search?q=read json in mysql.
Nothing for devicetype.
Can someone help me where am I wrong ?
you cant sent by url value like // or & so you can use urlencode for value like this
url=<?php echo urlencode('https://www.google.co.in/search? q=read+json+in+mysql&aq=f&oq=read+json+in+mysql&aqs=chrome.0.57j0l3j62l2.7010j0&sourceid=chrome&ie=UTF-8#hl=en&sclient=psy-ab&q=parsing+json+in+mysql&oq=parsing+json+in+mysql&gs_l=serp.3..0j0i22i30l3.138180.199873.3.200144.44.28.10.5.6.2.824.5388.1j22j1j1j1j0j2.28.0...0.0...1c.1.12.psy-ab.6dwtA9Be9BY&pbx=1&bav=on.2,or.r_qf.&bvm=bv.46340616,d.bmk&fp=cb8cca45920a2be6&biw=1920&bih=979
&devicetype=retina.') ; ?>

How to add a $userid onto an Html Link

Is it possible once I have retrieved a userid from a MySql database, for example, $userID, to then use that as a parameter in a HTML link, for example;
<href="http://www.site.com/?110938">
Thanks.
In PHP, anything you echo is sent to the browser. So you can simply echo that variable.
echo $userID;
Or to put it in the link:
<href="http://www.site.com/?<?php echo $userID ?>">
Of course that means you're trusting that $userID will contain valid data (and not some malformed HTML attempting to break your site and ruin your user's lives).
If it should always be a number, you can keep out potential bad data by forcing it to be an integer:
<href="http://www.site.com/?<?php echo (int) $userID ?>">
I believe what you are looking to do is use the GET method of retrieving data.
The GET method (as opposed to post) pulls all of the parameters out of the URL. For example, lets take a google maps search url:
http://maps.google.com/maps?q=Empire+State+Building,+NY&hl=en
Let us break down this URL:
http://maps.google.com/ - this is the location of the script
maps? - Maps is the name of the file that contains the script (if
they are using PHP); notice, there is a ? in the URL, indicating
that the GET method is being used. All information after the ?
pertains to the GET data.
q=Empire+State+Building,+NY& - this portion is the first GET
variable. They have decided to use the letter q as the name of
their GET variable, and the value is Empire+State+Building,+NY. The
& indicates that another variable will be specified.
hl=en - this is the second variable in their query.
If they were to use PHP to read these parameters, they would use the following code:
$var1 = $_GET['q']; //Has value Empire+State+Building,+NY as string
$vars = $_GET['hl']; //Has value en as string
If you wanted to have your user's ID be in the URL, and use that to interact with the database, the easiest way would be to use GET, such that your URL would look like this:
www.example.com/example.php?id=45828
Then in your script, you would use the following code to access that data and query the database with it:
$userid = $_GET['id'];
$query = "SELECT * FROM users WHERE userid=$userid;";
$query = mysql_escape_string( $query );
mysql_query( $query );
just pass the variable like <href="http://www.site.com/?var_name=<?php echo $userID;?>">

Categories