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

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

Related

PHP: obtain the url page that call my page 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.

Laravel Check Back to Site

I want to check redirect to another link from our webpage if user clicking on back from browser I must be alert for user such as 'Backword Forbidden ...'
I'm using this code and that not working for me:
$referer = Request::header('referer');
or how to check witch URL user backword to our site?
If you want to get the Referer URL, you can use either Request::header('referer') or native $_SERVER["HTTP_REFERER"]. But there are (at least) 2 problems with that:
It can be spoofed, empty etc.
It will only work if the person got to your page through a link. It won't work when pressing the browser's back button or backspace.
The function you're looking for is Request::server() which functions just like the $_SERVER super global, so to get the page referer you'd do the following.
$referer = Request::server('HTTP_REFERER');
Using Request::header('refer') will only work for POST requests.
GET requests are the one your're looking for.
You can use Request::segment(1) or Request::segment(2), depends on the exact URL you're using.

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/

php - How reliable is HTTP_HOST, and will it work for search bots?

I have numerous domains and rather than create content for each one individually, I'm using the following code in each index.php page to display the relevant page from another site I own (where 'getDomain' is a function for getting just the domain from a url):
<?php
$domain = $_SERVER['HTTP_HOST'];
$domain = getDomain($domain);
$crawl = "http://example.co.uk/page.php?domain=$domain";
$request = curl_init($crawl);
print curl_exec($request);
curl_close($request);
?>
Anyway, my questions are 1) is it reliable to use HTTP_HOST like this and 2) will search engine bots index my page or will they just get an error?
My hunch is that I'm better off entering the domain name into each index.php page - but I'd rather not have to do that!
Any help greatly appreciated!
This will work ok (and for search engines too), but can be realy slow... If you content and design not differs in diferent domains, better way is to configure normal web-server virtualhosts for that

PHP GET question - calling from a POST call

I have a quick question i hope you guys can answer, i've got a search system that uses POST to do searches, now i want to track queries using Google Analytics but it requires using GET url parameters to pull parameters out the URL, what i don't want to do is rewrite the entire search system to use GET instead of POST. Is there any way around this? I was thinking maybe i can make a GET call to a new page from the page that recieves the search POSTs, but i don't want it to redirect, i merely want it to "hit" the url without actually redirecting?
Is this possible?
Any other solutions would also be appreciated.
Thanks for the help
You can specify an abritrary URL when you add your GA code. For example, all our different checkout pages go through validate.php, so this is the URL that the person would see, however, we put in some extra code to give a specific tracking URL to google.
For example:-
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXX-1");
pageTracker._setDomainName("example.com");
pageTracker._trackPageview("/checkout/login/");
} catch(err) {}
</script>
Would make google track this as being /checkout/login/ even though the page in the browser actually shows /validate.php
You can output (as we do) this page variable from different PHP variables
$searchterm = $_POST['search'];
echo 'pageTracker._trackPageview("/search/' . urlencode($searchterm) . '");';
Sure, use the apache mod_rewrite module to create a fancy, seo friendly url and pass the user keywords in the url.
Something like "www.yoursite.com/search/what+a+user+searches+for/"
In a .htaccess file you create a rule
RewriteRule ^search/(.*)/$ /search.php?keywords=$1
You're orignal script keeps working with your postvalues and you provide an URL with GET variables. With explode("+", $_GET["keywords"]) you can extract the searchvalues.
With the array $_REQUEST you can access all request parameters GET and POST.
The only way you will be able to do this, is re-set the forms method to GET and just changed the $_POST requests to $_GET
Thats not such a huge change?
You should be able to do that with HTTPRequest:
http://php.net/manual/en/class.httprequest.php
You can just alter your Google Analytics code - see Tracking Custom Variables

Categories