Get URL by $_GET - php

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

Related

Receive Querystring and Redirect page

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

how to get passed in function from page url php

I am trying to pass the id to another page so it can be deleted when the link is pressed. the Id is displaying in the url but I am having trouble, getting hold of the id, I have tried: to use the pars url but it is not working.
parse url
echo parse_url( $_SERVER['REQUEST_URI'], PHP_URL_QUERY);
link
<p>Delete</p
It is because your URL needs to be corrected to add a query string, starting with a ? and an identifier
<a href="<?php echo 'example.php/?foo='.$item->getproductid(); ?>">
Now you can either parse the URL (not necessary and unnecessarily complex for this action) or get the value from the GET array:
$_GET['foo']; // is the id in $item->getproductid()

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)

Remove string from the link href

I'm using a jQuery pagination script and I'm using the onChange function so if a user click on the page number it does redirect him to the $_SERVER['REQUEST_URI'] + it adds an page number to the request url, but if I will click on some pages several times then the request url looks like this: &page=3&page=1&page=10 ... etc.
The code looks like this:
onChange : function(page){
window.location = '" . $_SERVER['REQUEST_URI'] . "&page='+page;
}
Now I need to remove $page=??? from the url if it already exist.
After this
$url = $_SERVER['REQUEST_URI'];
$url = preg_replace_all("/\\&page=[^\\&]+/", "", $url);
$url will contain the url barring the page attribute
The reason for this is that every time the user clicks on your link, the value of $_SERVER['REQUEST_URI'] is the current URL and you are just appending an extra string to the end.
You need to set the get variable to the page you want then just change this variable when your function is called. Something like:
$_GET["page"] = page;

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