Calling a PHP page once only

This section is for posting questions which are not directly related to WYSIWYG Web Builder.
Examples of off topics: web server configuration, hosting, programming related questions, third party scripts.

Note that these questions will generally not be answered by the administrators of this forum.
Post Reply
User avatar
alex4orly
 
 
Posts: 632
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

Calling a PHP page once only

Post by alex4orly »

I have my "homepage.html" page and need to call a PHP script that makes an entry into a log on the server for each visit.
I need the log to make only a single entry per visit / session.
I inserted the following code into the HTML page just before the /body> tag.

<div id="insert-file"></div>
<script type="text/javascript">
$(document).ready(function()
{
$('#insert-file').load('http://www.xxxx.xxxx/log.php');
});
</script>

It works fine and does the job, BUT - it keeps executing it each time the user comes back to the home page....
1) Is the above code OK?
2) How can I enforce it to only execute ONCE per visitor's session
User avatar
Pablo
 
Posts: 21572
Joined: Sun Mar 28, 2004 12:00 pm
Location: Europe
Contact:

Re: Calling a PHP page once only

Post by Pablo »

You will need to keep track of whether the user has visited the page, for example by using cookies.
User avatar
alex4orly
 
 
Posts: 632
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

Re: Calling a PHP page once only

Post by alex4orly »

I found out in the meantime something called "sessionStorage", below is my revised code which seems to do the trick.
I am aware that some browsers will not support this, but...
Will appreciate any comments on this solution?

Thanks for your help

<div id="insert-file"></div>
<script type="text/javascript">

if (!sessionStorage.previouslyVisited)
{
$(document).ready(function()
{
$('#insert-file').load('http://www.xxxx.xxxx.xx/log.php');
});
sessionStorage.previouslyVisited = true;
}

</script>
User avatar
Pablo
 
Posts: 21572
Joined: Sun Mar 28, 2004 12:00 pm
Location: Europe
Contact:

Re: Calling a PHP page once only

Post by Pablo »

This looks correct.
WWBman
 
 
Posts: 916
Joined: Fri Jan 08, 2010 6:10 pm

Re: Calling a PHP page once only

Post by WWBman »

Sessionstorage is cleared when the browser is closed so if a user is like me and closes their browser every time they have finished surfing then you will get more entries.
Also I think whenever a new tab is opened (or refreshed?) a new session is created, not sure about that.
It's worth considering and testing if it's a concern for you.
Post Reply