how would i do this?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lizlazloz
Forum Commoner
Posts: 64
Joined: Mon Dec 29, 2003 7:29 am

how would i do this?

Post by lizlazloz »

how would i make it so that a page can only be viewed once a day by a user?

i would get the ip addresss...

then how would i check it had not visited this page before today? adding all ips that visit the page into a MySQL table then checking cant eb the best way, surely?
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

cookies. or using user/pass

if you use ip address you will block people that uses the same ip (corporate users... etc..)
lizlazloz
Forum Commoner
Posts: 64
Joined: Mon Dec 29, 2003 7:29 am

Post by lizlazloz »

ah, cookies, good idea. people could delete them... but hey, they'll do for the monent.
lizlazloz
Forum Commoner
Posts: 64
Joined: Mon Dec 29, 2003 7:29 am

Post by lizlazloz »

ok well so far I've got this:

Code: Select all

<?php 
if( ! $_COOKIE("link") == "ok" ) 
{ 
$conn=mysql_connect("localhost","shaun","squall") 
or die("Could not connect"); 

$rs = mysql_select_db("game", $conn) 
or die("couldnt select database"); 

$sql="update game set strength= strength + 1 where id=1"; 

$rs=mysql_query($sql,$conn) 
or die("could not execute query"); 

$sql="select id,name,strength from game where id=1"; 

$rs1=mysql_query($sql,$conn) 
or die("could not execute query"); 

while( $row = mysql_fetch_array($rs1) ) 
{ 
$msg="You just clicked <b>".$row[name]."'s</b> secret link!  "  .$row[name]."'s strength has  increased to <b>".$row[strength]."</b>"; 

} 
setcookie("link","ok", time()+86400 ); 
} 
else 
{ 
$msg="You can only click this link once a day!"; 
} 
?> 
<html><head><title>Page</title></head> 
<body> 
<?php 
echo($msg); 
?> 
</body></html>
and i get " Array to string conversion on line 2" (this line: if( ! $_COOKIE("link") == "ok" ) )

why? whats wrong with my code?
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Use a database to record each IP/date/time/browser and possibly use [php_man]sessions[/php_man] aswell.

Sessions have an edge over cookies in this case, because it's better that the webmaster controls the information rather than the client to prevent double accessing.

Also that line is wrong because arrays use square brackets not parenthesis. Make it $_COOKIE['link'].
Post Reply