Page 1 of 1

Calling a PHP page once only

Posted: Wed Nov 29, 2017 12:21 am
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

Re: Calling a PHP page once only

Posted: Wed Nov 29, 2017 6:58 am
by Pablo
You will need to keep track of whether the user has visited the page, for example by using cookies.

Re: Calling a PHP page once only

Posted: Wed Nov 29, 2017 7:22 am
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>

Re: Calling a PHP page once only

Posted: Wed Nov 29, 2017 7:52 am
by Pablo
This looks correct.

Re: Calling a PHP page once only

Posted: Wed Nov 29, 2017 9:18 am
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.