I used to have a script that basically base64 encoded the link and then the redirect PHP page would decode it and send you off to the page.
I don't know what I am doing wrong and PHP is not my best skill, just looking for some help.
Link on page:
Test
find.php:
< ?php
$request_id = $_GET ['shop'];
$site = base64_decode($request_id);
header( 'Location: $site' ) ;
?>
If you want to use $variables in strings, use double quotes:
header("Location: $site");
Or concatenate the strings:
header('Location: '.$site);
More info here
+++ But better store that value into $_SESSION, because users can change it and it will bring up errors.
If you copied and pasted that second code, try removing the space between < and ?. It should be <?php.
Related
I wish to create a simple php script which takes 2 variables and redirect to a certain URL format,
e.g. http://example.com/redirect.php?folder=orange&ID=19
will redirect to say http://123.122.1.12/folder=orange&ID=19
where "orange" and "19" are the two variables (no space)
another thinking is that I can pass the whole string "folder=orange&ID=19" as one variable but it contains = and & so I don't know if it is possible as it may confuse the system when used in the URL,
Can someone give me a script (redirect.php) that can do the above please? thanks!
You are probably looking for http_build_query function, so you can do it this way:
header('Location: http://123.122.1.12/?' . http_build_query($_GET));
or
header('Location: http://123.122.1.12/?' . $_SERVER['QUERY_STRING']);
after more research and trial and error, the easiest solution is parse the whole string as one variable as in this URL
example.com/redirect.php?sx=folder%3Dkodak%26ID%3D19
<?php
header( 'Location: http://127.0.0.1/?'. $_GET["sx"] ) ;
?>
the key is to encode the = and & symbols in the URL so they can be seen as a string not codes.
I want to use the GET method to send a string to the receive page, but if the string includes '#', the receiver page can only get the sub string before the '#'.
As the following example:
test
When I click the 'test' link to open the 'test.php' page, which has the following code:
<?php
if(isset($_GET["q"])) {
echo $_GET["q"];
}
?>
It only display 'string1' on the page, '#string2' is missing.
So I want to know what happened to the string, and how to fix this problem.
Thank you for any help!
=======Update===========
With the help of #Eric Shaw and #JP Dupéré, I know how to fix this problem.
The simplest way is encoding the string before using the get method.
To encode the query string, you can:
use urlencode() in PHP, and urldecode() can decode the string.
use encodeURIComponent() in JavaScript, and decodeURIComponent() can decode the string.
Try
urlencode("string1#string2")
before calling GET.
The #foo is used to jump to an <a name="foo"/> tag on the page, rather than viewing the top of the page when the browser loads it.
The stuff after the # is processed by the browser and NOT sent to the server.
You can escape the # and the escaped version will be sent to the server, i.e.
test
will do what you want I think
This escaping is also a common technique to get the # passed along in the URL for redirectors.
this is my code:
header("Location: ?pid='".$_GET['pid']."'");
die();
When I write a simple echo $_GET['pid']; the value is good but then when I introduce this variable in the header it return something like 27%27 and thats not true true value
When I use urlencode the probleme persist:
header("Location: ?pid=". urlencode($_GET['pid']);
Whats the problem here?
Thank you
This is because the parameter is being encoded into URL format. Read about urldecode() PHP function.
Also, the %27 is a URL encoded single quote char, therefore you need to remove single quotes from your code:
header("Location: ?pid=".$_GET['pid']);
If you still however will get %27 in your header, then I would suggest stripping it out from var by using trim() like this:
header("Location: ?pid=".trim($_GET['pid'], "'"));
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");
i have script
<?php
$to = $_GET["to"];
header("Location: $to");
?>
if i call script such
out.php?to=http://site.ru/page.php?param1=1¶m2=2
in param $to be only http://site.ru/page.php?param1=1&
how to fix? i want that $to = http://site.ru/page.php?param1=1¶m2=2
You can escape the URL at the site calling out.php:
Go to $to
& is a reserved character in an URI. When you access this URL, ¶m2=2
is interpreted as belonging to the current URL and not to the value of to.
If you want to transmit it literally, you have to encode it with %26:
http://site.ru/page.php?param1=1%26param2=2
Most programming languages provide a function to do so. (e.g. JavaScript, PHP). The best thing is to encode the whole URL.
$to must be urlencoded, but note that you giving a redirect script to anyone, so, any phisher can use it.
So, it would be better to store urls in the database and pass only an identifier.
try encoding the to URL in base64 and then in the example that u have shown decode it before you pass it to the header :)
urlencode it
urlencode($to)
I ran into the same problem before, this is what I did:
$arr=explode('?to=',$_SERVER['REQUEST_URI'],2);
$new_to=$arr[1];
Now you can use the $new_to variable.
Of course if you're using this for production environment, I would recommend encoding the url as the other answers advised. I was using it for testing curl script. getting the variable this way has lots of flaws, so be careful.
You can use a Function called "html_entity_decode"
Click Here for more information about this function
or use md5 function to encrypt the URL and then decrypt it when you put it into a varriable.
I hope this can help you