Solved

Counting number of page loads by visitor

Posted on 2011-09-14
3
300 Views
Last Modified: 2012-08-13
Hi,

I have a simple website with a SEARCH form and Every visitor can perform a search.

I want to be able to count the page loads to allow only 3 searches/loads by user, then, after 3 loads detected to  disable the search text field:

<input name="textfield" type="text" id="textfield" value="Search...">
 
<input name="textfield" type="text" disabled id="textfield" value="Search...">

Should this be done by creating cookies ? or using session variable ? or IP check ?

Could some expert please help me with this implementation and the condition for text field ?

Thank you

0
Comment
Question by:Fernanditos
  • 2
3 Comments
 
LVL 13

Accepted Solution

by:
themrrobert earned 250 total points
Comment Utility
use session variable
<?php
session_start();
//place at top of page /\

$_SESSION['count']++;

if ($_SESSION['count'] >= 3) echo $disabledtext; 
else echo $normaltext;

?>

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
It depends on whether you want the 3-count to persist beyond a single visit.  Session cookies will be lost when the browser is closed, so the session will not persist.  however you can set your own cookies that last as long as you want.
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
Comment Utility
Here is an example that will do what you want (and a few other things).  But there is a philosophical question you might want to ask yourself.  Why do you want to limit the searches?  Most sites would be only too happy to have clients visiting lots of times and making lots of searches.  Just a thought...
<?php // RAY_temp_fernanditos.php
error_reporting(E_ALL);

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

// SET THE SEARCH LIMIT
$limit = 3;

// ASSUME WE WANT TO ENABLE THE SEARCH
$disabled = NULL;

// SET THE SEARCH COUNTER IF IT'S NOT THERE
if (empty($_SESSION["search_counter"])) $_SESSION["search_counter"] = 0;

// DISABLE THE SEARCH IF THE COUNTER IS OVER THE LIMIT
if ($_SESSION["search_counter"] > $limit) $disabled = 'disabled';

// HOW MANY SEARCHES ARE LEFT
$n = $limit - $_SESSION["search_counter"];

// IF THERE IS A SEARCH
if (!empty($_GET['q']))
{
    $_SESSION["search_counter"]++;
    $n = $limit - $_SESSION["search_counter"];
    if ($n > 0)
    {
        $message = "YOU SEARCHED FOR {$_GET["q"]}";
    }
    else
    {
        $message = "SORRY, TOO MANY SEARCHES";
    }
}
// IF THERE IS NO SEARCH
else
{
    $message = "YOU DID NOT SEARCH";
}

// IN EITHER CASE
$message .= "<br/>YOU HAVE $n SEARCH(ES) LEFT";

// IF THERE ARE NO SEARCHES LEFT, DISABLE THE BOX
if ($n < 1)
{
    $disabled = 'disabled';
    $message  .= "<br/>SORRY, YOU CAN NOT SEARCH ANY MORE";
}


// CREATE THE WEB PAGE
$html = <<<HTML
<script language="JavaScript">
<!--
function searchIsBlank()
{
    value=document.forms['search'].elements['q'].value;
    if (( value == "" ) || ( value == "Search"))
    {
        alert("Please enter at least one word in the search box.");
        document.forms['search'].elements['q'].value = "";
        document.forms['search'].elements['q'].focus();
        return false;
    }
    else
    {
        return true;
    }
}

function searchClear()
{
    value=document.forms['search'].elements['q'].value;
    if (value == "Search")
    {
        document.forms['search'].elements['q'].value = "";
        document.forms['search'].elements['q'].focus();
        return false;
    }
    else
    {
        return true;
    }
}
</script>
<form  class="search" name="search" method="get">
$message
<br/>
<input class="search_input"  type="text"   name="q"  $disabled  value="Search"  onclick="return searchClear();" />
<input class="search_submit" type="submit"                      value="go"      onclick="return searchIsBlank();" />
</form>
HTML;

// WRITE THE PAGE
echo $html;

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now