Detect if navigated to by Domain Name or IP address - php

Is it possible to detect whether a user navigated to my site via Domain Name or by IP Address? Google isn't being very helpful.

Yes, you can eg. use client-side, in JavaScript (judging from the tags it is acceptable):
window.location.hostname
as per documentation: https://developer.mozilla.org/en-US/docs/DOM/window.location
Or server-side, in PHP:
$_SERVER['HTTP_HOST']
as per documentation: http://php.net/manual/en/reserved.variables.server.php

You can check this :
echo $_SERVER['HTTP_HOST']

Yes it is: In PHP you have the server-variables, and this one you need:
$_SERVER['HTTP_REFERER'];
This will give you the domain you visitor came from :)

Related

Is there a way to get a URL of a launching page in PHP

In PHP 5.6, if I am on a page A, and if page A launch page B, is there a way to get the URL of a page A from page B?
Please note that I do not want to use query parameters to pass the values of a URL A when I launch page B from page A.
If the browser (user agent) supports it, you could use:
$_SERVER['HTTP_REFERER']
'HTTP_REFERER'
The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.
From: http://php.net/manual/en/reserved.variables.server.php
You can check it using $_SERVER['HTTP_REFERER']. The HTTP referer header will return a string. If you don't know how to work with this you can try:
echo $_SERVER['HTTP_REFERER'];
If your user came by google page, $_SERVER['HTTP_REFERER'] will give you http://www.google.com
And, of course, check the documentation: http://php.net/manual/en/reserved.variables.server.php
If launch means include or require, then you can get URL with this (printing example):
echo $_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
$_SERVER['HTTP_HOST'] will print base URL and $_SERVER['SCRIPT_NAME'] will print the remaining part (subfolders and file name), like:
http://example/web/pageb.php
I have tested this to make sure it works. But be sure to escape as there could be vulnerabilities here. It also works if $_SERVER['HTTP_REFERER'] is null in your case.

check if page was redirected or opened directly in php cross-domain

How can I check if I was redirected from another domain to page or opened directly in right domain?
Thanks for answer!
I assume from the tags, which you assigned that you own an server, running PHP and want to know whether the users, visiting your page are comming from a page belonging to your domain or from somewhere else.
This is normally stored in the referer header of an HTTP request.
Try accessing it in PHP with $_SERVER['HTTP_REFERER']
The variable should contain the whole path of the source page and you can extract the domain/hostname using parse_url()
Complete example:
<?php
$sourcehost = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
?>
I tested it but ufortunately, after redirect 301 there is no data stored in the $_SERVER['HTTP_REFERER'] variable.

How can I make a link to the same page on a different site?

So I have a test version of my site. In the header of the test server, I'd like to include a link to same page on the live server.
Is there an HTML or PHP means of "knowing" what the current page is?
Click me!
or alternatively,
Click me!
With PHP you can use:
$_SERVER['REQUEST_URI'];
A hyperlink would look like this:
Click for current page
Note that PHP_SELF will show only the filename, and not the GET params.
If the url is like: index.php?page=aboutus
REQUEST_URI would be index.php?page=aboutus
while PHP_SELF would be index.php
Take a look at: http://php.net/manual/en/reserved.variables.server.php
There is JavaScript's window.location - Object, which makes many useful information available. There is also
$_SERVER['REQUEST_URI']
on the PHP side, as mentioned in the other answers.
Suppose that your test server is http://test.site.com, and your live server is http://site.com, then you could just use an absolute URL:
thepage.html on live server
In PHP, $_SERVER variables will get URL parts. In JavaScript, document.URL is the place to start. :)

Is there a way for a PHP script to know the address of the site that it is used on?

I have a PHP script located on mysite.org and I use it to display images on sub sites of example.com (like example.com/foo/bar, example.com/foo/another-bar). I can only post links to that script there.
Tried $_SERVER['REQUEST_URI'], and $_SERVER['REMOTE_ADDR'] but there's no go.
So what i need is some way to know if its used on e.g. example.com/foo/bar.
It sounds like you're looking for the Referer header, which tells you the page that the request for an image came from.
Note that some browsers do not send this header.
just create a test php script containing:
<?php
phpinfo();
?>
It will give you very detailed list of php settings and global variables, the setting that you are looking for is bound to be there.
You need a combination of $_SERVER['SERVER_NAME'] and $_SERVER['REQUEST_URI']. Or maybe I'm misinterpreting the question. $_SERVER['SERVER_NAME'] returns the domain name a script is running on.
You may use $_SERVER['SERVER_NAME'], you can check other variables from the php documentation: http://php.net/manual/en/reserved.variables.server.php
I think you are looking for $_SERVER['HTTP_REFERER']

how can i get http://www value in php

i want to save the whole URL in my database
i get the domain name using
=$_SERVER["SERVER_NAME"]
but how can i get it along with http://www
you write it manually
"http://www.".$_SERVER["SERVER_NAME"];
What about this? :
$self = "http://".$_SERVER["SERVER_NAME"].$_SERVER["PHP_SELF"]."?".$_SERVER["QUERY_STRING"]."";
You can even get the arguments after the page name
http://www.webcheatsheet.com/PHP/get_current_page_url.php
Don't forget running on possible non standard ports. And always test to see if your request was done through https, don't test for port numbers to determine ssl.
$uri = "http".($_SERVER['HTTPS']?'s':'')."://".$_SERVER['SERVER_NAME'].(($_SERVER['HTTPS']&&$_SERVER['SERVER_PORT']!='443')||(!$_SERVER['HTTPS']&&$_SERVER['SERVER_PORT']!='80')?":".$_SERVER['SERVER_PORT']:"").$_SERVER['REQUEST_URI'];
$_SERVER['SERVER_NAME'] already contains the "www", if that was the requested host. All you need to append is the protocol (http:// or https://).
You can determine which the site was accessed through by looking at the port -- $_SERVER['SERVER_PORT'] will be 443 for https://

Categories