Is there a way to prevent a php page from being loaded more than once by the same visitor?

Hi Guys,
I'll try to put this in the simplest terms I can.
I've created a php page that (when it's loaded in the browser) updates a MySQL db.
That part functions just fine.

The trouble is every time that page is refreshed by the visitor it updates the db with additional data. Every time!

So, is there a way to ensure it only updates the db once per session?
Or maybe prevent the page from being refreshed or reloaded by the visitor?
Thanks,
Sas
LVL 1
sasnaktivAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
You can cookie the browser and check the cookie to avoid the DB update.  This article shows a general design to use a cookie for a once-only page.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_3314-How-to-Show-an-Introductory-Web-Page-Once-Using-PHP.html

If you want to tell us a little more about it there might be a better solution.
0
COANetworkCommented:
Drop a cookie with the session ID on the visitor, check it when page loads, if your database already has an entry with that session id - don't insert.  And/or set the cookie to expire after a certain time, if you want to be time-based.  then you can only count the visit if the cookie is expired / does not exist.
0
Dave BaldwinFixer of ProblemsCommented:
Common practice with form pages is to post the data to a PHP page that updates the database and redirects to the next page.  That way, the page and code that updates the database is never seen in the browser so a refresh or back never causes it to update again.

Form page ---> Update page  ---> Result page.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Ray PaseurCommented:
And if you're worried about multiple submits, this can help prevent the problem.  It just depends on exactly what you're trying to accomplish / prevent.  You might want to install this script and experiment with it.

<?php // RAY_multi_submit.php
error_reporting(E_ALL);


// PREVENT MULTIPLE SUBMISSIONS DUE TO REPEATED REFRESH, CLICKS ON SUBMIT BUTTON, OR FIRING THE BACK BUTTON
// EXAMPLE:
//    if ( multi_submit() )
//    {
//       handle error
//    }
//    else
//    {
//       normal processing
//    }


// ALWAYS START THE PHP SESSION ON EVERY PAGE
session_start();



// A FUNCTION TO RETURN TRUE OR FALSE ABOUT MULTI-SUBMIT CONDITIONS
function multi_submit($type="POST")
{
    // MAKE THE FUNCTION WORK FOR EITHER GET OR POST SUBMITS
    $input_array = (strtoupper($type) == "GET") ? $_GET : $_POST;

    // GATHER THE CONTENTS OF ALL THE SUBMITTED FIELDS AND MAKE A MESSAGE DIGEST
    $string = NULL;
    foreach ($input_array as $val)
    {
        // CONCATENATE ALL SUBMITTED VALUES
        $string .= $val;
    }
    $string = md5($string);

    // IF THE SESSION VARIABLE IS NOT SET THIS IS NOT A MULTI-SUBMIT
    if (!isset($_SESSION["_multi_submit"]))
    {
        // SAVE THE SUBMITTED DATA MESSAGE DIGEST
        $_SESSION['_multi_submit'] = $string;
        return FALSE;
    }

    // IF THE SESSION DATA MATCHES THE MESSAGE DIGEST THIS IS A MULTI-SUBMIT
    if ($_SESSION['_multi_submit'] === $string)
    {
        return TRUE;
    }
    else
    {
        // SAVE THE MESSAGE DIGEST TO DETECT FUTURE MULTI-SUBMIT
        $_SESSION['_multi_submit'] = $string;
        return FALSE;
    }
}



// SHOW HOW THIS IS DONE
if (!empty($_POST))
{
    if (multi_submit())
    {
        die("ALREADY GOT THAT");
    }
}



// CREATE THE FORM FOR THE DEMONSTRATION
$form = <<<FORM
<form method="post">
ENTER SOMETHING, THEN REENTER IT
<input name="foo" />
<input type="submit">
</form>
FORM;

echo $form;

Open in new window

HTH, ~Ray
0
sasnaktivAuthor Commented:
Hi Guys,
I've come up with yet another approach. Been trying to make it work but having problems with my unreliable iMac. I'll keep you posted on how it works.
Sas
0
Ray PaseurCommented:
OK, if in the future you choose to fill us in on the details maybe we can add value.  Best of luck with your project, ~Ray
0
Dave BaldwinFixer of ProblemsCommented:
Interesting.  If you are working with form submissions, the method I described above is commonly used in many programming languages, not just PHP, because it exactly does the job.  And it is used on thousands, maybe even millions of pages.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sasnaktivAuthor Commented:
Yes, Dave Baldwin's approach is exactly what I needed. But Ray's information taught me about cookies which will be very useful in different situations.
Thanks guys,
Sas
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.