Receive Querystring and Redirect page - php

How can I get a QueryString named "code" and redirect my page including the querystring?
Example-
I'll receive mydomain.com/inteface.php?code=103103
I need redirect for mydomain_new.com/interface.php?code=103103.
I know C#, but in this case I will need this code in php for redirect in a different server.

header('Location:mydomain_new.com/interface.php?code='.$_GET['code']);

You can use the superglobals called $_GET & $_SERVER. You can use $_GET['code'] to grab the code variable from the current URL and $_SERVER['HTTP_HOST'] to grab the domain like this:
//Grab code from URL
$code = $_GET['code'];
//Grab current Domain Name being used
$currentURL = $_SERVER['HTTP_HOST'];
//Old Domain Name
$oldDomain = "mydomain.com";
//Read the header of the URL to test $domain is TRUE and $code has data
if ($currentURL == $oldDomain && isset($code)) {
//Redirect to new domain using $_GET
header('Location: http://mydomain_new.com/interface.php?code=$code');//No need to concatenate single variable
}
See:
http://php.net/manual/en/reserved.variables.get.php
http://php.net/manual/en/reserved.variables.server.php

Related

How to retrieve original url in a redirected page using PHP

Let say I have redirect rule on url example.com which redirect to newurl.net.
How can I get original url on newurl.net in order to be used and handled with some details or just to be printed on new destination.
Thank anyone for help.
If it's not external You can use
$_SERVER['HTTP_REFERER']
And it will give you the referer which is what you want.
See Acquiring referral after a PHP redirect for more informations.
But if you are trying to get to another website, the browser will overwrite the headers so you need to do it some other way.
You can store it in session.
//Save it in your first website
$_SESSION['REFERER'] = $_SERVER['HTTP_REFERER'];
And then in the other website :
//Use it in the other
$referer = '';
if (isset($_SESSION['REFERER'])) {
$referer = $_SESSION['REFERER'];
unset($_SESSION['REFERER']);
}
You could also pass the referer as a parameter:
header('Location: http://newurl.net?original_referer=' .$_SERVER['HTTP_REFERER']);

Get URL by $_GET

I need get current url (url can be different), I want send this url to email by ajax form, but I cant get right url. I use:
echo $_GET['ref']
But it return empty value.
Addition:
I have ajax form which send some data (including current url) to my email.
Yes, I use this 2 values:
$url1 = $_SERVER['HTTP_HOST'];
$url2 = $_SERVER['REQUEST_URI'];
But it return me url like this:
/cloud/abuse/abuse_mailer.php
not my current URL.
$_GET is an array containing data passed through a GET request method. If you want to get your current URL, then you can use $_SERVER; it contains server and request data. As an example:
echo $_SERVER['REQUEST_URI']; // outputs current URI of client
Or you can try:
echo $_SERVER['PHP_SELF'];

How can I get current page name including $_GET variable in URL?

Let's say I have the page:
index.php?page=page-title-here
I want to get the current page name including the $_GET variable in the URL.
I am currently using this:
basename(__FILE__)
It outputs "index.php", the actual file name. Any idea how to also include the $_GET variable so that it will output "index.php?page=page-title-here"?
The variable $_SERVER["REQUEST_URI"] gives you the file with GET parameters. Also includes folders in the url.
Edit: Use $page = end(explode('/', $_SERVER["REQUEST_URI"])); if you want to get rid of the folders from the url.
You can do so using the REQUEST_URI:
echo $_SERVER['REQUEST_URI'];
From the manual:
REQUEST_URI: The URI which was given in order to access this page; for instance
Try...
$page = (__FILE__) . '?' . $_GET['page'];
Try $_SERVER['REQUEST_URI'] (there are lots of interesting things in $_SERVER)
Use:
basename($_SERVER['REQUEST_URI'])

i want previous page URL with GET parameters

Currently i using this ,
$page=urlencode($_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
which gives url without get parameters, but i need the previous page URL with GET parameters
Use HTTP_REFERER that referred the user agent to the current page
$page=urlencode($_SERVER['HTTP_REFERER']);
You have to use the Referrer header.
$_SERVER['HTTP_REFERER']
Please, keep in mind, a user can change the value of headers, so you can't "trust" it.
$url ="";
if (isset($_SERVER['HTTP_REFERER'])){
$url = $_SERVER['HTTP_REFERER'];
}
if want to send post get this as encryption the use
if (isset($_SERVER['HTTP_REFERER'])){
$url = urlencode($_SERVER['HTTP_REFERER']);
}
if you want to decrypt the url
$url = urldecode($url);
Simply used
$_SERVER['HTTP_REFERER']
You will get whole URL (with get parameters)

problem getting redirect to work properly php

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.

Categories