Jump to content

Views Counter only add this once each time not refresh.


Gayner

Recommended Posts

Like I have a row in my table called "views'

 

and in my php code

 

When i view my prayer, i want it only increment my views using this code:

 

update table set views=views+1 where id=$id

 

But only once per session for each prayer :-) So people don't spam views up u know ?

will this work

 

if(empty($_SESSION['viewed'])){
  $_SESSION['viewed'] = true;
  echo "hey";
  mysql_query("UPDATE prays set views=views+1 WHERE id={$row['id']}");
}
else
echo "already viewed";

 

I am sorry but i am so lost on sessions, how'd u get $_SESSION['viewed']

 

can we just create our own names for sessions? lmao

 

cause right now it doesn't update

 

i got over 200 prayers rows how does this work for each one seperately?

Where does $row['id'] come from ?

 

Think of sessions as a global array.

 

you should have something like this

 

session_start();
if(!empty($_GET['debug'])) $_SESSION['viewed'] = false; //debug

if(empty($_SESSION['viewed'])){
  $_SESSION['viewed'] = true;
  echo "hey";
  $SQL = sprintf("UPDATE prays set views=views+1 WHERE id=%d LIMIT 1",$row['id']);
  mysql_query($SQL) or die($SQL."\n".mysql_error());
}else{
  echo "already viewed";
}

 

pass &debug=true in the URL to override the "already viewed" check

Where does $row['id'] come from ?

 

Think of sessions as a global array.

 

you should have something like this

 

session_start();
if(!empty($_GET['debug'])) $_SESSION['viewed'] = false; //debug

if(empty($_SESSION['viewed'])){
  $_SESSION['viewed'] = true;
  echo "hey";
  $SQL = sprintf("UPDATE prays set views=views+1 WHERE id=%d LIMIT 1",$row['id']);
  mysql_query($SQL) or die($SQL."\n".mysql_error());
}else{
  echo "already viewed";
}

 

pass &debug=true in the URL to override the "already viewed" check

 

rowid from my mysql

 


$query="SELECT `id`,title,prayer,time,level,nameid,name,prays FROM prays WHERE id = {$_GET['view']} ORDER by id DESC";
$result=mysql_query($query);
while($row=mysql_fetch_array($result))
{

 

it's just id from table,

i dont wanna use _GEt to override that will look ugly on the url, i know some sites have in built in..

Like I have a row in my table called "views'

 

and in my php code

 

When i view my prayer, i want it only increment my views using this code:

 

update table set views=views+1 where id=$id

 

But only once per session for each prayer :-) So people don't spam views up u know ?

 

Lol ur Script didn't even help me people just used &Debug=true to keep spamming.

 

here is the final product, works flawless, reduces 80% of views

 

 

 

// anti flood protection
if(isset($_SESSION['lol']) && $_SESSION['lol'] > time() - 3){
    // users will be redirected to this page if it makes requests faster than 2 seconds
    echo "";
exit;
}
else 
echo "hey"; 
  $SQL = sprintf("UPDATE prays set views=views+1 WHERE id=%d LIMIT 1",$row['id']);
  mysql_query($SQL) or die($SQL."\n".mysql_error());
  mysql_close();
  
$_SESSION['lol'] = time();
}

 

WIN WIN!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.