Well, I don't know if I made a title properly but I've got a lil php script out there and I am wondering if there might be possibility when user go to this url:
siteurl.com/index.php?token=J55OSGTTk3W7mRcuq54006w7ROv
to get automatically only /index.php without the content that he copy/pasted in this case
?token=J55OSGTTk3W7mRcuq54006w7ROv
Hope it is possible. Cheers.
Yes. Issue a Location header and use parse_url() to get the parts you need.
$url = "http://siteurl.com/index.php?token=J55OSGTTk3W7mRcuq54006w7ROv";
$parsed = parse_url($url);
$redirect = $parsed["scheme"] . "://" . $parsed["host"] . $parsed["path"];
header("Location: $redirect");
Maybe explode($url,"?")[0]. If there's no question mark, then it should return an array of length one. If there is, you'll only get everything before the '?'.
Related
my code:-
$host = "https://example.org/get.php?";
$newurl = file_get_contents($url);
$newurl = substr($newurl,stripos($newurl,"who"));
$newurl = substr($newurl,0,stripos($newurl,"</"));
// newurl string starts with "who" and ends with "</"
//var_dump($host.$newurl)."<br>"; //shows correct request string
header("Location: " . $host.$newurl);
exit();
new window displays the correct url request in the address bar but isn't redrawn
page source is blank except a single "1" char.
on pressing the resubmit button the page is drawn correctly.
Any help greatly appreciated.
Stve
You can change the file_get_contents($url); to $host because it's not correct source url
Firstly - If other people have a similar problem I would urge them to check that what they think is being offered up to file_get_contents($url) and to header("Location: $host$newurl"); is correct. Not is my case.
Is this case the PHP was interfacing with my java server which it did in two ways the file_get_contents($url); updated the database and returned a new url. Then the header("Location: $host$newurl"); showed the updated page.
Secondly - make sure the server is doing what you expect. In this case the server had closed the socket after the file_get_contents($url); so could not respond to the header("Location: $host$newurl");
Even old hands fall into the trap of believing their code is correct!
Thanks all for you reponses.
Steve
Think my current page url is:
http://example.com/id=10
In this this page has link to go other page, I want to pass current URL as a query string like this:
http://example.com/about-us/?edit=1&return=http://example.com/id=10
in PHP
http://example.com/about-us/?edit=1&return=<?php echo $_SERVER['QUERY_STRING'] ?>
but this is not working, could anyone help me to do this.
Use this (I assume you are using http only);
$currentUrl = urlencode("http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
$link = "http://example.com/about-us/?edit=1&return=" . $currentUrl;
use urlencode($_SERVER['QUERY_STRING'])
It encodes the link.
I am trying to use a php script that redirects visitors based on certain criteria. I use the script succesfully on an apache server however, I am experimenting with nginx and php-fpm and the same script doesn't seem to be working as it should.
header("Location: $url");
exit();
The strange thing is it appears to be appending the URL I am trying to redirect to to the original URL so the URL it tries to forward to looks like:
originaldomain.com/redirectdomain.com.
Has anybody ever come across this before where it as appending the redirect domain to the original URL instead of redirecting straight to it?
Please let me know if you need any further information to help.
You need to make sure the URL has http:// at the beginning of it, otherwise it thinks it's going to a path on your domain, and not a redirect to an actual site.
$url has to begin with http:// or https://
if (strpos($url, 'http') === 0 ) {
$newurl = $url;
} else {
$newurl = "http://" . $url;
}
Then just use $newurl in you header request :)
If the above answers are not yet fixed header redirect, you need to check in your php.ini file output_buffering on or off or set limit.
output_buffering = On
I currently have my htaccess file set to remove the file type so
www.example.com/home.php
would become,
www.example.com/home
I need to compare the url with one that I have saved in the database, I do this by,
if ($_SERVER['QUERY_STRING']) {
$pageURL = str_replace('.php', '?', 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'] . $_SERVER['QUERY_STRING']);
}
else {
$pageURL = substr('http://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'], 0, -4);
}
When I save the url in the database it gets saved as the current page url after the htaccess file has removed the .php, it is inserted using jquery ajax.
When I come to compare it, I need this to make it match in php as php doesn't take into account the htaccess changes.
Is there a better way to do this?
You can do:
$pageURL = rtrim($_SERVER['REQUEST_URI'], '.php');
This would give your url with .php removed whenever it appears at the end of the url.
Look into the parse_url() function. You can use it to parse the URL into parts then modify the path component (e.g. remove .php). Then you can rebuild the URL from those parts using the http_build_url() function.
Seems to me that combining John's suggestion above of using parse_url() along with using pathinfo() would be best -- trimming, truncation with substr, etc. isn't always as elegant as pathinfo().
I have a little problem with redirecting. Registered users follows this link site.com/reg.php?passkey=1234 but the first the user get redirected to the correct language based on a cookie. I need to keep the passkey variable when the user is redirected. like this ?lang=en_US&passkey=1234
My code so far look something like this:
if (!isset($_GET['lang']))
{
if (isset($_COOKIE['country']))
{
$country = $_COOKIE['country'];
(...)
elseif ( $country == "US" ){
$variables = $_GET;
$variables['lang'] = "en_US";
header('Location: ?' . http_build_query($variables));
exit();
}
This works:
reg.php
reg.php?lang=en_US
reg.php?lang=en_US&passkey=test
reg.php?passkey=test&lang=en_US
but this gives an The page isn't redirecting properly error
reg.php?passkey=test
I don't understand why this doesn't work when all the other combinations seem to work perfectly.
The HTTP 1.1 Specification requires that the location needs to be a Absolute URI
(See RFC2616 14.30 Location)
The location header('Location: ?' . http_build_query($variables)); does not contain an absolute URI.
You need something like:
header('Location: /folder/file.php?'.http_build_query($variables));
If you need to do this on different locantions you can use $_SERVER['PHP_SELF'] to set the current file as redirect location. For example
header('Location: '.$_SERVER['PHP_SELF'].'?'.http_build_query($variables));
I think, you should change the http_build_query($variables) to http_build_query($variables, null, '&')
I hope my answer is useful.