Solved

Counting number of page loads by visitor

Posted on 2011-09-14
3
326 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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 110

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

742 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