• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

[PHP] Script request

Skilled Illusionist
Joined
Apr 28, 2005
Messages
378
Reaction score
6
HELLO!

one thing first

something.com/cool.php?somthing
Lets name "somthing" wrote in bold in this message as - cyryptor
ok? :D

Now, lets say someone goes to adress
somthing.com/cool.php?omg1

Now this page says just - Hello!

now someone else goes to address
somthing.com/cool.php?stfu2

Page still says just - Hello!

and so on, untill 25th guest comes and visits site
somthing.com/cool.php?omg1own

Now site says - Bye!
But not only!

Also it saves that "cyryptor" this time "omg1own" to a text file.

Then this "Counter" goes back to zero and does like before untill another 25th visitor, and then again script wrotes into text file this "cryptor"

lets say other 25th user enters "cyryptor" as "omg1owntoo"

then text file will look like:

Code:
omg1own
omg1owntoo



I hope its not too hard :D and I hope yall understand what I need:)

Thanks in advance!

-If anyone want's to talk about it in MSN then my address is juunior@ragezone.com
 
Custom Title Activated
Loyal Member
Joined
Aug 8, 2004
Messages
3,892
Reaction score
20
First of all, get clear what you need:

1. Page must check every reload how many times it has been loaded.
2. If reload number is 25, page must save URI.

For both parts you need to save data. There are two commonly used methods, first being a file where you save data in, second being a database. Now, saving data to file is somewhat of a witch IMHO (since you need to protect the file or else it'll bug, so you'll need to secure open it etc), so I'll show you how its done with MySQL. Do note that the closing bracket (the closing equivalent of "{") on Ragezone is replaced by two stars (**).

Code:
<? 
session_start (); 
if (!isset ($_SESSION['counted'])) { 
   updateCounter(); 
   $_SESSION['counted'] = 1; 
** 

function updateCounter(){
  $query = mysql_query("UPDATE db_counter SET dc_count = 'dc_count' + '1'", getDB());
  $select = mysql_query("SELECT dc_count from db_counter");
  $count  = mysql_fetch_assoc($select);

  if($count['dc_count'] == '25') {
    $uri = checkVar($_SERVER["REQUEST_URI"]);
    $update = mysql_query("UPDATE db_counter SET dc_count = '1'", getDB());
    $save    = mysql_query("INSERT INTO db_URI (du_time, du_URI) VALUES (NOW(), '$uri')");
  **
**

function checkVar($variable) {
  global $error;
  $variable = htmlentities($variable);
  if(!get_magic_quotes_gpc()) $variable = mysql_escape_string($variable);
  return $variable;
**

function getDB() {
    static $instance;
    if(!isset($instance)) {
      // connect to the DB
      $instance = mysql_connect("localhost", "dbUsername", "dbPassword") or die("could not connect to the db");
      mysql_select_db("databaseName", $instance) or die("could not select db");
    **
    return $instance;
**


?>

Haven't tested it myself, so might contain a few bugs, but if it works it has:
- fast check for visiter count
- sessions, so it'll only count unique visiters
- variable cleaning, so you're not vulnerable for SQL injects ed.
- saved the URI of every 25th visiter as well as the time of the visit.

Do note you need to have a MySQL database for this to work, with 2 tables: db_counter, with the column dc_count type int and db_uri with the columns du_time type datetime and du_URI type varchar(50). I've included a database connection function, fill in your username/pass or simply use your own database connection function :)

I'll attach the source in a sec.
 

Attachments

You must be registered for see attachments list
Last edited:
Skilled Illusionist
Joined
Apr 28, 2005
Messages
378
Reaction score
6
Thanks frag! :)

Edit: You forgot to close braces :p

and I get this error
Fatal error: Call to undefined function: updatecounter() on line 4

Edit2: ok, got that fixed
 
Last edited:
Skilled Illusionist
Joined
Apr 28, 2005
Messages
378
Reaction score
6
It doesnt seem to count :( - Cant you take out that it only counts unique visitors? Thanks :)
 
Custom Title Activated
Loyal Member
Joined
Aug 8, 2004
Messages
3,892
Reaction score
20
Did you make the database and editted the database connector?

Since the unique visitor thingie works for sure if you get "Fatal error: Call to undefined function: updatecounter() on line 4", seeing how that means it already tries to update the counter.

Error in that is btw most likely the uppercase C in counter :D
 
Skilled Illusionist
Joined
Apr 28, 2005
Messages
378
Reaction score
6
No..

I think
Code:
Fatal error: Call to undefined function: updatecounter() on line 4

Camed because
Code:
   updateCounter();
is before
Code:
function updateCounter(){
  $query = mysql_query("UPDATE db_counter SET dc_count = 'dc_count' + '1'", getDB());
  $select = mysql_query("SELECT dc_count from db_counter");
  $count  = mysql_fetch_assoc($select);


And I did make databases, and set correct connecting data..
Maybe thing is that I look visitor count at wrong place at phpMyAdmin ?

and can I take unique user counter out by taking these lines out ?(So it counts every hit, not unique visitor):
Code:
session_start (); 
if (!isset ($_SESSION['counted'])) { 
   $_SESSION['counted'] = 1; 
**
 
Last edited:
Custom Title Activated
Loyal Member
Joined
Aug 8, 2004
Messages
3,892
Reaction score
20
Juunior said:
No..

I think
Code:
Fatal error: Call to undefined function: updatecounter() on line 4

Camed because
Code:
   updateCounter();
is before
Code:
function updateCounter(){
  $query = mysql_query("UPDATE db_counter SET dc_count = 'dc_count' + '1'", getDB());
  $select = mysql_query("SELECT dc_count from db_counter");
  $count  = mysql_fetch_assoc($select);
No it isn't. Learn to code. You can call a function way before its declared, as long as its declared in the same file (at least for PHP. With C / C++ you'd be correct, but that isn't the issue here).

And I did make databases, and set correct connecting data..
Maybe thing is that I look visitor count at wrong place at phpMyAdmin ?

and can I take unique user counter out by taking these lines out ?(So it counts every hit, not unique visitor):
Code:
session_start (); 
if (!isset ($_SESSION['counted'])) { 
   $_SESSION['counted'] = 1; 
**
No, if you do that you won't count any visiters at all.

Instead, replace

session_start ();
if (!isset ($_SESSION['counted'])) {
updateCounter();
$_SESSION['counted'] = 1;
**

with

updateCounter();

Dunno why it isn't working yet.. This is development section, not 'place your script request here and hope someone gives you a working script'.
 
Last edited:
Back
Top