Solved

Counting number of page loads by visitor

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
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 …

728 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