PHP Detect where page is loading from - php

I've searched and searched google for an answer but could not find one.
I need a PHP script or javascript ( PHP would be better but i don't mind ) to detect if a page is being loaded from SERVERIP/page.php or SERVERDOMAIN.com/page.php.
I have the server IP that does not cache the pages, And i have the domain that caches the pages. I want to make sure that if the page is being loaded from SERVERDOMAIN it gets redirected to SERVERIP so it does not get the cached page.
Example.
I go to SERVERDOMAIN for one page i get the cached page, I then go to another page that does not need to be cached so it gets redirected to SERVERIP.

See if
>$_SERVER['HTTP_REFERER']
works for you.

Related

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.

Save URL of AJAX loaded page, so it can be loaded after a refresh

We have an application writted in PHP. Its main view is for example: /pages/index.
Now when the user clicks on certain links, it pulls in other Views via ajax. ie. a call may look like /pages/publish, so the PHP outputs the relevant html for the publish section back to the index view.
The problem we have is we'd like to be able to give the user the option of refreshing and seeing the same view as before. So, my initial thought is this, when we use .load() in jQuery, to take the URL its going to load and store it somewhere to be read by the PHP if the user refreshes. Is the best way to do or can someone think of a better way to do this whole thing?
Check out jQuery.address which should solve your problems! It allows AJAX loading of new pages, and will update the address bar accordingly. If a user saves this URL and reloads it, the script on the page can then load the correct page.
Alternatively, if you're HTML5-only, then you can try history.pushState() which will modify the URL without using the hash symbol, but support isn't 100% yet. (I don't think... it certainly behaves oddly on iPad from my experience.)

why does my website insert two records when code is only run once!

Simply put, I am trying to create a custom logger for visitors to a php site with mysql. In doing so, I found that I was inserting two entries for every single page load.
I have already ruled out the code running twice by accident.
I put the query on every page, assuming it would work flawlessly. After realizing it was causing two entries to occur, I was confused and removed the query from the other pages, leaving it only on the index page.
Utilizing PHP's $_SERVER['HTTP_REFERER'] and $_SERVER['PHP_SELF'], I inserted the values into the log. To see where I was beforehand and where I was at currently.
So let's say I was on the create page, and I clicked a link to the 'home' page named index.php. The entries in the database would say referer was 'create.php' and the current page was 'index.php'. However, a second entry would be inserted that said referer was 'index.php' and the current page was 'index.php'
I checked what would happen when I clicked a link to a page without the logger, so I clicked to go to the 'create' page. I checked the database and there was another entry, but ironically only one entry was inserted this time. This was weird because the create page has no code to log a visitor and it was only inserting one entry. However, it was saying that the referer was 'create.php' and current page was index.php. Which is backwards from what it should be.
Doing more research, I decided to look into watching the headers live. So I checked using firefox and IE, where I realized that the headers get the page I'm requesting and also always gets the root page as well. For example, a request to create.php returns..
GET /create.php HTTP/1.1
GET /style/ HTTP/1.1
GET / HTTP/1.1
I understand the first GET, for the page I request, I understand the second GET for the external stylesheet. The last one is what confuses me, although it seems to corroborate why I'm getting the duplicate entry.
Since, there is no visitor log on the create.php page there is no first entry to the log, but once it loads the root page, the entry is created for the create.php being referer and the current page is the index.php page because it is loading the root page.
Since, there is a visitor log on the index.php page, when I go from 'create.php' to 'index.php', the first GET creates an entry of 'create.php' being the referer and current page being 'index.php', but a second entry says that 'index.php' is the referer and the current page.
So I am pretty certain it is this reasoning for which I get a second entry.
Can anyone tell me why it is loading the root page on every request?
I'm using xampp for mac os x locally, and I understand it might be the default favicon. However, I just removed the icon and it still occurs, even though the favicon doesn't display.
I can supply code samples and other things, it just seems irrelevant at this point because the insert query works, it just occurs twice when I clearly only execute it once.
The "GET /" request is pulling index.php automatically.
There is probably an invalid image SRC url, or script SRC, or link HREF, somewhere on the page. Those can cause regular-looking requests against the server in the background without realizing it. Review all your HTML for URLs and ensure they're all valid server-side files. There could also be a server-side redirect for unknown files back to the home page.
That or you have some JavaScript (potentially ads?) that's doing an ajax request.
We'll need full source to really trace this down.

Detecting when a link has been clicked with PHP

I have built an in browser engine that will retrieve pages without executing server side scripting... seems ridiculous, I know, but I'm doing this as part of a school project.
The problem that I am having is that once it displays the page if a link is clicked it will bring you to www.their-site.com instead of www.my-site.com?site=www.their-site.com.
Basically I need my php page to detect if a link is clicked and, if so, add "www.my-site.com?" before it so that all sites will still be rendered without all the server side scripting. Is their any way to do this?
---------------EDIT---------------------------------------------------------------------------
Ok I guess I wasn't clear enough the first time sorry about that.
I have made a php page that will display the contents of any site without executing the server side scripting that belongs with that page. This allows you to get around those annoying news articles that allow you to have a glimpse at them for two seconds and then a login box appears. the problem is once you've accessed the pages if you click any links you are connected to their server and the scripts turn back on. I want MY php to execute, not THEIRS
You need to know what you want first.
You say no server side scripting, then you mention php.
To do this, I don't think you can do it with just js.
You need to get the pages, using php, depending on what exactly, modify them such that when a link is clicked, it sends an ajax call to another page. This will require either regex replacement or the use of htmldom.
When a link is clicked, it should send the ajax response to the php page which can then request tha page, make modifications and send it back to the browser. You can then use js to replace the page contents.

How to tell clicking "back" to load the cache?

I realize that you can't 100% know that this will work in all browsers. All I care about is IE 8, Chrome, and Firefox. I need some base headers that I can put at the top of my PHP pages to allow the Forward/Back buttons to load the cache.
Update: on every page I have a logged in user box at the top of the page which gives the user access to their account.
I'm looking for a performance increase in the web site. The user having to reload the site when clicking back/forward creates unnecessary the server load.
Edit: After extensive research into caching and my level of knowledge I do not know a good solution. It also appears that most others don't know either.
What you want is jumping back in the page cache. There are various variables that determine if a page is put into the page cache.
Surfin' Safari has recently written a blog about the page cache. In short a page isn't put into it if:
The page is not completely loaded or uninteresting. (404 errors…)
The page is complicated to halt. This is the case if it has Flash elements or movies.
The page has frames.
The page is secure.
The page has an unload event. In their second blog post they write about alternatives.
Those are the rules Webkit follows, I don't know if it's documented for the other browsers. You can never be sure that a page is put into the page cache. It's best to design websites that don't relay on behavior that can change without notice.
Are you using sessions? If so, before calling session_start() call session_ cache_ limiter('') .
When you call session_start() the cache limiter is reset to the value set in session.cache_limiter (which by default is 'nocache'). If you want your pages to cache you need to explicitly set the cache limiter to something else first.
You can experiment with other values (see http://www.php.net/manual/en/function.session-cache-limiter.php) but I find session_ cache_ limiter('') gives the best result when trying to, for example, prevent forms from being reset when sending a user back after an error.
Although keep in mind this may open privacy concerns if your pages hold personal data, as personalized pages will become cached.

Categories