i want previous page URL with GET parameters - php

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)

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'];

Send URL as a Query String

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.

Change URL using PHP

e.g. i have page with url http://mysite.com?page=3&var=10 also there is form on page.
When form submitted there some actions in php but i need to remove this ?page=3&var=10 after form was submitted somehow is there way compatible with all browsers trough PHP without mod_rewrite?
This is an old topic, but just in case anyone else is searching for this in the future, you can use the javascript replaceState to change the history and browser bar label. A simple php function to do this:
function set_url( $url )
{
echo("<script>history.replaceState({},'','$url');</script>");
}
Then would simply call this function with the desired url (presumably dropping the post variables):
set_url("http://example.com");
A page reload or a back after calling another page will now have the new url location in the history.
I think that using POST may be a more elegant solution, but if you must use GET this is a work around.
If you're using action=index.php, then all values will be posted to index php, ?page=3&var=10 will be automatically removed.
If you want to post to the same page you can either use 'action=index.php?page=3&var=10' or action=<?php echo $_SERVER['PHP_SELF'] ?>
You can check at the beginning of the page if something submitted and then redirect to whatever you want with header('Location: http://www.example.com/'); More about header function http://php.net/manual/en/function.header.php
Yeah, the solution is quite simple (even if not really SEO friendly):
<?php
header("Location: http://mysite.com")
?>
just for information...why do you need it?
use parse_str to get the query string as an associative array that is easy to modify. Then use http_build_query to convert the associative array into a query string.
$queryString = $s['QUERY_STRING'];
$params = array();
parse_str($queryString, $params);
//change $params as needed
$queryString = http_build_query($params);
if ($queryString) {
$queryString = '?'.$queryString;
}
return preg_replace("/\\?.*/s","",$s['REQUEST_URI']).$queryString;
preg_replace("/\\?.*/s","",$s['REQUEST_URI']) removes the original query string allowing you to replace it.
Does this work for you?
header('Location:/');
mod_rewrite cannot affect what's displayed in the user's browser address bar, UNLESS the rewrite does an externally visible redirect. Otherwise it only rewriting things within the webserver, and that's invisible to the user.
If you want to affect the user's address bar, you'll have to do a redirect via header('Location: ...') after the form's finished processing.

How to get the current full url (url + fragments) of the webpage in PHP?

I want to fetch the current URL which I've received as a callback during authorization from Salesforce.com. The data in the url is in the fragments portion.
You can use a combination of $_SERVER['HTTP_HOST'] and $_SERVER['REQUEST_URI'] but HTTP_HOST is not very reliable so try defining URL's for known sources and appending the request uri to the end.
define('URI', 'http://Salesforce.com');
echo URI. $_SERVER['REQUEST_URI'];
You will not be able to get the fragment portion of the URI back as it is used on the client only. If you need this you will manually have to send them to a server with some JavaScript.
With the $_SERVER variable, you could do this:
echo $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
Take a look at the following page for all available keys: php.net
I belive its something like $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING'];
http://php.net/manual/en/reserved.variables.server.php

Categories