PHP: obtain the url page that call my page php - php

I have an HTML page that calls my page php.
I want via php to obtain the URL that calls me.
I have tried $_SERVER['REQUEST_URI']; but it returns me the URL of my page.

$_SERVER['HTTP_REFERER'];
Please note that this value is not 100% sure (it's possible for the client/users to modify it), but it's still what's closest from what you want.

Related

Redirect a web page URL to another website

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);

Obtain browser url value in php

How to get address in the browser using php.
I want a way in which I can fetch the url value that is present in the browser. If I manually add a #tag to the existing url then I want to retrieve that as well.
I have used this code till now, but I want to retrieve https or http whatever value is in the browser.
Also this is my url:
http://example.com/xyz/?p=65
but suppose I build up the 2nd url manually then I would like to retrieve that as well
http://example.com/xyz/?p=65#fsgsg
$Path=$_SERVER['REQUEST_URI'];
echo $URI= 'http://'.$_SERVER['SERVER_NAME'].$Path;
The part behind the # is not delivered to the browser. You could however run a tiny javascript that sends you that information since it is available to the DOM (But do you really want that?) via the window object.
For getting has parameter,use below --
$url = 'http://amitbera.com/path?arg=value#anchor';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_PATH);
More details in http://www.php.net/manual/en/function.parse-url.php
Also,For gettting arg value use $_SERVER['QUERY_STRING']
Your only option is to handle that parameter in javascript because the # (hash) part wont get sent to the backend side, You can just detect click of the target element in JS and then glue the # part as a parameter like '&hashValue=fsgsg'.
I hope that helps You in some way.

Get URL variable in 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.

Get $_SERVER['HTTP_HEAD'] through an iframe?

I want to know if it's possible to get the $_SERVER['HTTP_HOST'] of a page calling the PHP script through an iframe.
For instance: index.html having and have test.php get the HTTP_HOST of index.html
If that makes any sense :P
Google/Bing/etc has been absolutely worthless on this and I can't be the only new-ish-to-PHP person wondering :(
context: I'm trying to figure out the simplest way to display an ad on a site. If at all possible, I'd like to be able to send everything using an iframe calling a PHP script, then send different ads depending on which site the request is coming from :)
You can use $_SERVER['HTTP_REFERER'] to find out where the iframe content has been called from.
You could then for example make use of parse_url to find the hostname
$parsed = parse_url($_SERVER['HTTP_REFERER']);
$domain = $parsed['host'];

What's the easiest way to redirect to the previous page with PHP?

I'm using a form to submit some post information to a PHP script. After the script finishes, I want it to redirect right back to the page the user came from. Right now I'm just using header() with a static URL. I've found a ton of very conflicting information about this around the internet, so I'm wondering what StackOverflow thinks.
Use HTTP_REFERER:
header('Location: ' . $_SERVER['HTTP_REFERER']);
access the $_SERVER['HTTP_REFERER'] variable and redirect to this. Should do the trick.
the way i would do it is use a session variable to store the current page URL everytime it is accessed.
$_SESSION['last_url'] = <get current url>
replace your static url in the header with $_SESSION['last_url']. Depending on how you implement your PHP, you can use search google for "current url php" or just $_SERVER['REQUEST_URI'] (stackoverflow doesnt allow me to put more than 1 link!)
Use REQUEST_URI But watch out for that presiding slash/

Categories