Solved

Counting number of page loads by visitor

Posted on 2011-09-14
3
320 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 109

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 109

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
This article discusses four methods for overlaying images in a container on a web page
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ā€¦
This video teaches users how to migrate an existing Wordpress website to a new domain.

820 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