PHP: check from which page the link is directed - php

lets say i have a page hit.php in my website and the link is to this page is available on every\many pages in the website..
lets say i click <a href"hit.php">HIT</a> from any page.....
is there a way when hit.php is loaded i could know that the page from which this page was..
meaning if i click the link to hit.php from 'index.php' i could check loading hit.php that the user is navigating here from 'index.php' or any other page...
i know this i could establish in site by passing variables in URL like 'hit.php?pagename=index' but is there another way
p.s i know its crazy but still :)

$_SERVER['HTTP_REFERER'], although it can easily be spoofed or disabled.

You can use the referer. In PHP, it's:
$_SERVER['HTTP_REFERER']
However, users can configure their browsers not to send this, or spoof it. You should ensure that your code doesn't break if it's absent, or faked.

Related

How to disable direct url unless linked from another page?

I have a page called snow.php. I want to disable people from being able to see my webpage if they type in www.mysite.com/snow.php unless they are linked from another page. So if I have another page called about.php and link to snow.php they'll be able to see it.
What you want is how to prevent hotlinking. Here is a small tutorial: http://www.hongkiat.com/blog/smarter-way-to-prevent-image-hotlinking-with-htaccess/
This method isn't 100% reliable, but you can use $HTTP_SERVER_VARS['HTTP_REFERER'] to get the current page that the user came from to access snow.php.

How do I find out that a link was seen

I'm trying to make a page with some links and when somebody clicks on a link, the score count will go up.
How can I find out the visitor who has really seen the page related to link? But not just click the link and close the page for score...
really seen means: page loads completed.
and my links opens in new window.
any solution?
You cant really see pages that aren't in the same domain. Chrome even puts them in a separate thread.
Back in the day you could have used a CSS exploit talked about here: https://developer.mozilla.org/en-US/docs/Web/CSS/Privacy_and_the_:visited_selector
If you really want to make a page with this kind of functionality you will have to make a browser plugin/extension.
You can include a nonce token in the link, and post that token to the server, render the page embedding that same token in some javascript and have the javascript post back the token when the page is done rendering. Seems kinda overkill though.
The only thing I could thing of is maybe make the link to like a redirect page on your site and then you could control to see if the page was loaded and then like after the page was loaded redirect to the actual webpage to link is intended. This way you know for sure that the user waited to view the webpage.
Other than that I don't think there is any other way for you to go about this.

Activate external href onclick from link

Let's say you have a link on your site and when you click that link it brings you to a different site. A site that you do not own and I would like it to have a "onclick" already activated maybe from a script that redirects to that link. Is this possible?
Simple answer NO. If possible it would be called 'mousejacking' :)
Setting window.location.href to a target link URL in javascript will, for most browsers, redirect the browser to the specified URL.
If you're specifically trying to make a link on your site redirect the user to a Google Search, read here.
There is almost no good reason to wrap an external site that you do not own in a frame. That kind of stuff is usually considered shady/malicious.

Is it possible to trace browser history in a PHP code?

I want to know that whether on a php page we can get browser history?
Suppose I want to display a php page if a specific link on that browser has been visited earlier otherwise a blank page is to be displayed.
Is it possible to do so??
As it is your own site, you can store a session variable to confirm they went there, do something like
session_start();
$SESSION["VisitedMySpecialPage"]=time();
and then in the checker page
session_start();
if (isset($SESSION["VisitedMySpecialPage"]))
{
// check here it was within say the last hour..
}
You can track what pages where visited on your own domain, but you can't check what other domains he visited. You can check the referer header to see what site he came from , but that's only 1 site and isn't very reliable.
Well, there was workaround to this problem some time ago, I don't know which browser still can do it:
place <a> tag with href attr to link what you are interested on your
inspect <a> node (via JS), if that link got :visited pseudo class send
send result via ajax to server

Is it possible to dynamically change the URL of a webpage?

I just wanted to know, is there any possible way to change the URL that appears in the address bar of a webpage dynamically? Like, maybe there are two buttons on the webpage, and when the user clicks one it will (or won't it does not matter) refresh the page and the url will be mysite.com/page1, or if the user clickes the second button, the url that appears in the address bar will be mysite.com/page2?
I do not need it to chaneg the domain, just the part after.
Just wanted to say, that I DO NOT want to go to another page. This must be done on one page. It does not matter if it is done with JS, PHP, or via the .htaccess file, but it must do this with only one page.
Outside of changing the .location you only really have control over the window.location.hash.
window.location.hash = "boo"; http://mysite.com -> http://mysite.com/#boo
This is the only way to not go to a new page while changing the URL. All other methods will refresh the page or redirect the page:
window.location redirects user when changed
window.location.pathname redirects user when changed
window.location.search redirects user when changed
window.location.hash does not redirect user when changed
You can also just change the non domain path by using a relative url:
window.location = "page1"; // include forward-slash if necessary
// goes to http://somesite.com/page1
You can definitely (and easily) serve the same page off both /page1 and /page2 and have the buttons navigate respectively to one and the other -- "refreshing the page", as you say (i.e. loading it up again from the server, or browser cache), and of course change accordingly what appears in the address bar, too. However, I don't see what's the point of doing that.
I don't quite understand what you want.
Is it that in both cases, you want the same page to be shown but with different urls ?
In that case, you could write a .htaccess file to redirect to the same page for both /link1 and /link2 and point the button to either of the links.
Just to update this question in case others come along.
This can now be handled via javascript using pushState(). There's a couple of libraries (such as History.js) that aim to ease implementation across different browsers currently without proper support. However, if you'd like see a simple usage example without the use of such libraries, feel free to check out the following article on Hawkee
Dynamically change URLs using Push and Popstate

Categories