I want to link links on my website via go.php?urlhere, I have been told and I have tried using go.php?url=urlhere however the URL's to which I redirect, redirect to another URL with-in it for example go.php?http://.com/click?p=0&a=0&url=http://.com, many of the redirect I have tried to use simply copy the URL in the go.php file and use a meta refresh or a window.location reload; however they redirect to the second URL and not the first one. Sometimes when I do actually get it to redirect the first part of the redirected URL gets all the dots changed to "_" which stops it redirecting.
I want to have something like this website using on its "Buy It Now" buttons
http://www.searchchief.co.uk/Search/Sony-PSP-Go
So I think this is what you are asking: How do I redirect to a page using a url as a query string parameter?
eg.
http://www.myurl.com/go.php?url=http%3A%2F%2Fwww.myotherurl.com
You must do two things to achieve this on the url:
have a query string parameter. eg.
go.php?url=mysite.com or
go.php?redirect=mysite.com not
just go.php?mysite.com.
you must URL ENCODE this query
string parameter value. eg. go.php?url=http%3A%2F%2Fwww.myotherurl.com
NOT go.php?url=http://www.myotherurl.com. In php you can use the urlencode() function or http://meyerweb.com/eric/tools/dencoder/ can do it for you if you have a small number of urls to convert.
On the PHP side of things you will use the following
<?php
header('Location: '.urldecode($_GET['url']));
exit();
?>
You can do other basic checks on the php side of things eg. check for a valid url format etc
I think you want to use the header command.
So, in your case you would do this:
For a url in this syntax: go.php?url=thisurl
This would be the code
<?php
header('Location: ' . $_GET['url']);
?>
Related
I have a simple PHP website. Let's call it youtubex.com. I want to redirect youtubex URLs (in the format shown on STEP2) to my website in the format shown on STEP3. Here, I am using YouTube, just for illustration.
STEP1: https://www.youtube.com/watch?v=lj62iuaKAhU
STEP2: https://www.youtubex.com/watch?v=lj62iuaKAhU
STEP3: https://www.youtubex.com/#url=https://www.youtube.com/watch?v=lj62iuaKAhU
STEP1 shows any desired URL. STEP2 shows the same URL from STEP1 with youtubex as domain. STEP3 shows the final required URL. I am trying to redirect STEP2 to STEP3.
I tried finding some solutions to this on the internet and SO, but, none help. Here is one.
This should do the work:
RedirectMatch 301 /watch$ https://www.youtubex.com/#url=https://www.youtube.com/watch
An inefficient but full php solution can be using the location header in php :
$vid = $_GET['v']
if($vid){ header("location:https://www.youtubex.com/#url=https://www.youtube.com/watch?v=$vid");
}
by the way you can't get the text after the hash mark in php because it is not sent to the server.
javascript can do it in a more neat way without the second reload by checking window.location.href to see if the hash does not exist already and then get the v parameter in url then change url without refreshing the page by using window.history.pushState({"html":response.html,"pageTitle":response.pageTitle},"", urlPath);
using php str_replace and header :
$step2_url = "https://www.youtubex.com/watch?v=lj62iuaKAhU";
$part2_url = str_replace("youtubex","youtube",$step2_url);//the output is : https://www.youtube.com/watch?v=lj62iuaKAhU
$step3_url = "https://www.youtubex.com/#url=".$part2_url; //the output is : https://www.youtubex.com/#url=https://www.youtube.com/watch?v=lj62iuaKAhU
now you have the final url , simply redirect
header($step3_url);
I am writing a PHP script that will redirect based on a link like this:
header_redirect($_GET['redirect']);
And the URL being:
https://www.url.com/index.php?change_language=english&redirect=other_page.php?category=1234&limit=24
As you can see, the actual page is
https://www.url.com/index.php?change_language=english
and the redirect is then to another page with multiple variables like:
&redirect=other_page.php?category=1234&limit=24
However, when I run the link above, I only get redirected to "other_page.php" and the additional variables are lost.
How would I achieve this?
You can solve this problem using some encryption and decryption trick, I have used base64_encode and base64_decode() function to solve your problem.
Step1: in your html page
<?php $redirectUrl = base64_encode('other_page.php?category=1234&limit=24'); ?>
Link1
Step 2: in your header_redirect() function, you can use base64_decode() function decode the redirect stings and get the expected one.
function header_redirect($redirect){
$redirect = base64_decode($redirect); // you can get the expected redirected url with query string
//your redirect script
}
Depends on what you're trying to do.
Option 1: Use http_build_query.
Try:
$target=$_GET['redirect'];
unset($_GET['redirect']);
$query_str=http_build_query($_GET);
header_redirect($target.'?'.$query_str);
If your starting URL is this:
https://www.url.com/index.php?change_language=english&redirect=other_page.php&category=1234&limit=24
You would then be redirected to:
https://www.url.com/other_page.php?change_language=english&category=1234&limit=24
Option 2: Use rawurlencode and rawurldecode.
However, if your goal is to be redirected to whatever you have stored in $_GET['redirect'] (and ignore any other variables in the URL), then you would need to encode the other_page.php&category=1234&limit=24 bit before you put it into your starting URL. This will effectively escape the special characters and allow you to simply call header_redirect(rawurldecode($_GET['redirect']));.
Say your starting URL is then:
https://www.url.com/index.php?change_language=english&redirect=other_page.php%3Fcategory%3D1234%26limit%3D24
You would then be redirected to:
https://www.url.com/other_page.php?category=1234&limit=24
I'm trying to create a intermediary page, i.e user clicks on a link that leaves the site a page tells you that you're now leaving the site.
An example link would look like this:
http://example.com/transitionpage.php?r=http://www.google.com
transitionpage.php then works with a simple
$redirectto = $_GET['r'];
header( "refresh:2;url=".$redirectto );
However I'm running into the problem that if the url you're redirecting to also has multiple GET parameters in it, the domain gets cut off at the first occurrence of &
So if the link was originally:
http://example.com/transitionpage.php?r=http://www.google.com?par=1&par=2
It would become:
http://example.com/transitionpage.php?r=http://www.google.com?par=1
Which is unfavorable.
How do I pass on the full URL via GET without it getting chopped off ? Do I have to escape it ?
You can do URL encoding: http://www.w3schools.com/tags/ref_urlencode.asp
Your get query would translate to: http://example.com/transitionpage.php?r=http%3A%2F%2Fwww.google.com%3Fpar%3D1%26par%3D2
These are the PHP methods you need: http://php.net/manual/en/function.urlencode.php http://php.net/manual/en/function.urldecode.php
Use:
$link = 'http://example.com/transitionpage.php?r='. urlencode('http://www.google.com?par=1&par=2');
Use the built in function for Encoding Urls
for example
urlencode("http://www.google.com");
for function refrence
Urlencode Php
I m in a situation where i am redirecting user to another page with following jQuery code
window.location = "/#/customer/email?isEmail=true&eid=1&template=2";
i have some url re-writing , and so complete url becomes is
https://demo.qa.com/#/customer/email?isEmail=true&eid=1&template=2
but in PHP when i try to get full page url using this
echo $_SERVER['REQUEST_URI'];
it just gives me this
/
i just want to get variable IsEmail
$_GET['IsEmail']
value in PHP page,
I think the
#
in between the URL is creating the problem, is there any way to get it, please advise..
The fragment is never sent to the server, so if you want access to the query parameters you need to bring them forward:
https://demo.qa.com/?isEmail=true&eid=1&template=2#/customer/email
^ ^
query fragment
The anchor fragment portion of the URL (anything after #) isn't sent to the server at all. It only lives client-side. The server has no knowledge of it, and therefore PHP has no knowledge of it.
If you want to do anything with the anchor fragment, you must do it client-side.
Let's say a user has bookmarked "http://www.example.com/login#/settings". If that user try to access this page when he is logged out, firstly i want him to redirect to login page and then to the bookmarked page using this method
http://www.example.com/authenticate/login?continue=http://www.example.com/login#/settings
NOTE:
I'm using MVC architecture
Are there any method rather than HTTP_REFERER?
When user enter http://www.example.com/login#/settings ,i want to read whole url including # anchor in my controller file and then only i can set url to
http://www.example.com/authenticate/login?continue=http://www.example.com/login#/settings
so how do i do it??
You cannot read the part after the # from within PHP. You need to use JavaScript for that. For example, you can use
window.location.hash
To locate the hash-part, if any (it will be '' if no hash, or '#something' if there is one). You can then send this to the controller as a hidden field inside the request.
Depends on how you want to present it to your user. For a simple redirection, use header to send the HTTP Location Header
header("Location: http://www.google.com/");
If you want to give your user some time to read a short message before redirecting them, then you can use header to send HTTP Refresh Header
header( "Refresh: 5; url=newpage.php" );
Edit: In order to capture the anchor, you will need to use JavaScript. That information is not available to PHP. In that case, if you use JavaScript to capture the anchor, you might as well write your redirection in JavaScript.
Edit 2: Perhaps the other option is, when you are passing the continue to your program, also send the anchor as another GET variable. So your URI might look like this:
http://www.example.com/authenticate/login?continue=http://www.example.com/login&anchor=settings#/settings
Then use $_GET['anchor'] and concatenate it to the value of $_GET['continue'] with a #.
$uri = $_GET['continue'] . "#" . $_GET['anchor'];