Solved

Counting number of page loads by visitor

Posted on 2011-09-14
3
305 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
ID: 36538117
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
ID: 36538301
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
ID: 36538721
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

New! My Passport Wireless Pro Wi-Fi Mobile Storage

Portable wireless storage to offload, edit, and stream anywhere.

High-capacity, wireless mobile storage designed to accompany professional photographers and videographers in the field to easily offload, edit and stream captured photos and high-definition videos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

920 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

12 Experts available now in Live!

Get 1:1 Help Now