Limit to 1 post per day

Ask about a PHP problem here.
Post Reply
User avatar
Leverkusener
Posts: 3
Joined: Wed Jan 29, 2014 10:59 pm

Limit to 1 post per day

Post by Leverkusener »

Hey there all im back, there was some problems with forum i am Leverkusen and i cant login anymore but now thats not the problem

[syntax=php]<?php
// upis para
if($_POST['tim1']!="" and $_POST['tim2']!="" and $_POST['tip']!="" and $_POST['kvota']!="" and $_POST['link']!=""){
$racun=$_SESSION['liga_user_id'];
$result = mysql_query("select * from clanovi_njihovi_racuni where id='$racun';");
$row = mysql_fetch_array($result);
$klikovi = $row['klikovi'];

$tim1=str_ireplace(array("\"","'","<",">"),"",$_POST['tim1']);
$tim2=str_ireplace(array("\"","'","<",">"),"",$_POST['tim2']);
$tip=str_ireplace(array("\"","'","<",">"),"",$_POST['tip']);

$kvota=str_ireplace(array(","),".",$_POST['kvota']);
$link=$_POST['link'];
$ip=$_SERVER['REMOTE_ADDR'];

$tim1=strtolower($tim1);$tim1=ucwords($tim1);
$tim2=strtolower($tim2);$tim2=ucwords($tim2);

$link=str_ireplace(" ","",$link);
$prvoslovo=str_ireplace("http://www.flashscore.com/","#",$link);
$linkDozvola=0;
if($prvoslovo[0]=="#")$linkDozvola=1;

if($klikovi>2 and $linkDozvola==1 and $kvota>1 and $kvota<100){
$result = mysql_query("insert into clanovi_njihovi_parovi set racun='$racun',ip='$ip',datum=curdate(),vrijeme=curtime(),tim1='$tim1',tim2='$tim2', tip='$tip', kvota='$kvota',link='$link';");
$result = mysql_query("update clanovi_njihovi_racuni set klikovi = klikovi-0 where id='$racun'");
echo "<script>alert('Vas par je prihvacen')</script>";
}
else if($klikovi<3)echo "<script>alert('Nemate dovoljan broj klikova za unos para(minimum je 3 klika po paru).')</script>";
else if($linkDozvola==0)echo "<script>alert('Vas link je neispravan. Link utakmice kopirajte sa http://www.flashscore.com .')</script>";
else if($kvota<1.01 and $kvota>99.99)echo "<script>alert('Kvota vam nije u opsegu od 1.01 do 99.99 .')</script>";
}
?>
[/syntax]
this is my php code for posts

currently its unlimited, i want to limit to 1 post per day.

the user must wait until 00:00 to post again
im not sure how would it look like with select query for the current day
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Limit to 1 post per day

Post by Temor »

There are many things you can do here. Most of them includes timestamps.

One way is making a new column for the users in your db and insert a timestamp in it every time the user posts, and then compare that with your servers local timestamp and do some math to figure out if the post was made today or yesterday.

Another way is adding a new column with a boolean value ( 1 or 0 ) and changing that to 1 if the user makes a post, and then at 00.00 every night you run a cron job to reset all bool values to 0.

I'm not sure where you're stuck. Do what you can and I'll help you.
User avatar
Leverkusener
Posts: 3
Joined: Wed Jan 29, 2014 10:59 pm

Re: Limit to 1 post per day

Post by Leverkusener »

i have columns datum(date)its curdate and vrijeme(time)its curtime
http://prntscr.com/2nsr4o
and my post does not contain 1 thing, there are 5 things http://prntscr.com/2nsrfz
$tim1 $tim2 tip $kvota and $link
and of course every new post has new id, but that post doesnt look like forum post, its not 1. contains 5things
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Limit to 1 post per day

Post by Temor »

It really does not matter at all what other information you store in your database. For this purpose only the timestamp is relevant.

You need to take your timestamp, which is just a long string of numbers ( 1391071823 ) and see if the date is today, if not, then allow to post.

I would do this simply by checking if the date of the timestamp is a larger or smaller integer than todays date.

Say your user posted on the 28th, at midnight exactly.
This would make his timestamp 1390867200.
If you format this into YearMonthDay like this:
[syntax=php]date('ymj',1390867200);[/syntax]
you get this numerical value:
140128
which is just 2014 01 28 in a compact form.

If your user then posts again on the 30th, he gets this timetstamp:
1391071823
Which, if you format again, turns into this:
140130

Now it's just a matter of comparing them to see which is largest.
If the date is smaller, it was made yesterday or before that, if it is the same, it was made today, and if it's larger, it was made in the future ( which would mean someone figured out a way to travel through time :) )


I need to rush off to work now, let me know if you need further explanation and I'll do my best once I get home.
User avatar
Leverkusener
Posts: 3
Joined: Wed Jan 29, 2014 10:59 pm

Re: Limit to 1 post per day

Post by Leverkusener »

so with this timestamp i can disallow them to post 2 times in a day
so what i need to do? do create a new column in table which inserts timestamp numbers?
put this on my code
date('ymj',1390867200); and change this numbers with $datum and $vrijeme (date and time)?
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Limit to 1 post per day

Post by Temor »

You need to create a new column in your posts table where you will store a unix timestamp.
When a user posts, you insert the current times into the table along with the rest of the post data.

When the user wants to post again, you check the timestamp in your database in the way I showed in the previous post, and see if the date is before today, or if it is today.

User makes a post on January 28th, you insert the timestamp into the database:
[syntax=php]$current_timestamp = time(); ( 1390867200 in this case )
insert into ... $current_timestamp [/syntax]

So when he posts next time you grab his latest post, checks the time and sees that he made the post on January 28th:
[syntax=php]
$post_timestamp = select timestamp...
echo date('ymj',$post_timestamp);[/syntax]
this will print
140128

then you check current date, which when I wrote the previous post was January 30th,
[syntax=php]$current_timestamp = time(); ( 1391071823 in this case )
echo date('ymj',$current_timestamp);
[/syntax]
this will print
140130

then you check if todays date is bigger than the date in the database.
[syntax=php]if ( date('ymj',$current_timestamp) > date('ymj',$post_timestamp) ) {
allow to post
} [/syntax]
User avatar
leverkusen
Posts: 69
Joined: Sun Nov 18, 2012 10:09 pm
Location: Belgrade
Contact:

Re: Limit to 1 post per day

Post by leverkusen »

so this is only to limit them to 1 post per day? what about if i want to limit them to 10 posts, and then if i wish, i simply change the post limit number to 5?
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Limit to 1 post per day

Post by Temor »

Yes, the code I helped you wish only checks whether or not the person has posted in the last 24 hours or not. It does not count how many posts he's made. To do that you would have to create a new table in your database with a column that increments by 1 every time a user posts, and then resets back to 0 every 24 hours, or something along those lines.
User avatar
leverkusen
Posts: 69
Joined: Sun Nov 18, 2012 10:09 pm
Location: Belgrade
Contact:

Re: Limit to 1 post per day

Post by leverkusen »

I still need help for this, if needed i will explain what i need exactly. :(
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Limit to 1 post per day

Post by jacek »

I'm not totally sure I understand the problem but you can get the number of posts a user has made in the last 24 hours using something like this.
SELECT COUNT(1) FROM `posts` WHERE `user_id` = ? AND `post_time` > DATE_SUB(NOW(), INTERVAL 1 DAY)
Image
User avatar
leverkusen
Posts: 69
Joined: Sun Nov 18, 2012 10:09 pm
Location: Belgrade
Contact:

Re: Limit to 1 post per day

Post by leverkusen »

I will try to explain again.
I want to restrict unlimited posting. With this, everybody can post a football or basketball game.
I want to have a number, 2, 3 or 1, and after they try to get over that number, they obtain error *You reached limit post of '3' or
'2'*
If you need translation of my words in the script, i will give it to you.
<?php
// upis para
if($_POST['tim1']!="" and $_POST['tim2']!="" and $_POST['mtime']!="" and $_POST['tip']!="" and $_POST['kvota']!="" and $_POST['link']!=""){
	$racun=$_SESSION['liga_user_id'];
	$result = mysql_query("select * from clanovi_njihovi_racuni where id='$racun';");
	$row = mysql_fetch_array($result);
	$klikovi = $row['klikovi'];
	
	$tim1=str_ireplace(array("\"","'","<",">"),"",$_POST['tim1']);
	$tim2=str_ireplace(array("\"","'","<",">"),"",$_POST['tim2']);
	$mtime=str_ireplace(array("\"",";","/","-"),":",$_POST['mtime']);
	$tip=str_ireplace(array("\"","'","<",">"),"",$_POST['tip']);
	
	$kvota=str_ireplace(array(","),".",$_POST['kvota']);
	$link=$_POST['link'];
	$ip=$_SERVER['REMOTE_ADDR'];
	
	$tim1=strtolower($tim1);$tim1=ucwords($tim1);
	$tim2=strtolower($tim2);$tim2=ucwords($tim2);
	
	$link=str_ireplace(" ","",$link);
	$prvoslovo=str_ireplace("http://www.flashscore.com/","#",$link);
	$linkDozvola=0;
	if($prvoslovo[0]=="#")$linkDozvola=1; 
	if($linkDozvola==1 and $kvota>1 and $kvota<100){
		$result = mysql_query("insert into clanovi_njihovi_parovi set racun='$racun',ip='$ip',datum=curdate(),vrijeme=curtime(),mtime='$mtime',tim1='$tim1',tim2='$tim2', tip='$tip', kvota='$kvota',link='$link';");
	}
	else if($linkDozvola==0)echo "<script>alert('Vas link je neispravan. Link utakmice kopirajte sa www.flashscore.com .')</script>";
	else if($kvota<1.01 and $kvota>99.99)echo "<script>alert('Kvota vam nije u opsegu od 1.01 do 99.99 .')</script>";
}
?>
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Limit to 1 post per day

Post by jacek »

I'm not sure what some of those word mean but you need to do a check using the query I posted above.

Perhaps something like this
$result = mysql_query("SELECT COUNT(1) FROM `clanovi_njihovi_parovi` WHERE `racun` = {$racun} AND `datum` > DATE_SUB(NOW(), INTERVAL 1 DAY)");
$row = mysql_fetch_array($result);
$total_posts = $row[0];

if ($total_posts >= 3){
    // Show an error message
}else{
    // Insert the new post
}
Image
User avatar
leverkusen
Posts: 69
Joined: Sun Nov 18, 2012 10:09 pm
Location: Belgrade
Contact:

Re: Limit to 1 post per day

Post by leverkusen »

I have a problem making this script work fine. I dont know how to compile it properly :D
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Limit to 1 post per day

Post by jacek »

Compile it? PHP scripts are not compiled, they just run each time the page gets loaded.
Image
User avatar
leverkusen
Posts: 69
Joined: Sun Nov 18, 2012 10:09 pm
Location: Belgrade
Contact:

Re: Limit to 1 post per day

Post by leverkusen »

No no you missunderstand me i dont know how to make this script work, my bad. I dont know how to sort commands properly to get this working :D
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Limit to 1 post per day

Post by jacek »

Oh sorry. Okay so which part are you stuck with at the moment? Maybe if we break it down a bit it will make more sense.
Image
User avatar
leverkusen
Posts: 69
Joined: Sun Nov 18, 2012 10:09 pm
Location: Belgrade
Contact:

Re: Limit to 1 post per day

Post by leverkusen »

This is how my code looks now.
Im stucked at the point of limiting, i dont know what should i do first, where should i put select query if a user posted today and check if he posted. I dont know how to write that and where to write it. I tried with $today but its not working, as you see in the new code.

[syntax=php]<?php
// upis para
if($_POST['tim1']!="" and $_POST['tim2']!="" and $_POST['mtime']!="" and $_POST['tip']!="" and $_POST['kvota']!="" and $_POST['ulog']!="" and $_POST['link']!=""){
$racun=$_SESSION['liga_user_id'];
$result = mysql_query("select * from clanovi_njihovi_racuni where id='$racun';");
$row = mysql_fetch_array($result);
$profit = $row['profit'];
$klikovi = $row['klikovi'];


$tim1=str_ireplace(array("\"","'","<",">"),"",$_POST['tim1']);
$tim2=str_ireplace(array("\"","'","<",">"),"",$_POST['tim2']);
$mtime=str_ireplace(array("\"",";","/","-"),":",$_POST['mtime']);
$tip=str_ireplace(array("\"","'","<",">"),"",$_POST['tip']);

$kvota=str_ireplace(array(","),".",$_POST['kvota']);
$ulog=str_ireplace(array(","),".",$_POST['ulog']);
$link=$_POST['link'];
$ip=$_SERVER['REMOTE_ADDR'];
$tim1=strtolower($tim1);$tim1=ucwords($tim1);
$tim2=strtolower($tim2);$tim2=ucwords($tim2);

$link=str_ireplace(" ","",$link);
$prvoslovo=str_ireplace("http://www.flashscore.com/","#",$link);
$linkDozvola=0;

$today = mysql_query("select * from kurpal4_988.clanovi_njihovi_parovi where (clanovi_njihovi_parovi.datum >= current_date()) and clanovi_njihovi_parovi.racun= 'racun' ");

if($today==1);
if($prvoslovo[0]=="#")$linkDozvola=1;
if($linkDozvola==1 and $kvota>1 and $kvota<100 and $_POST['ulog']<=$profit){
$result = mysql_query("insert into clanovi_njihovi_parovi set racun='$racun',ip='$ip',datum=curdate(),vrijeme=curtime(),mtime='$mtime',tim1='$tim1',tim2='$tim2', tip='$tip', kvota='$kvota',ulog='$ulog',link='$link';");
}
else if($_POST['ulog']>100 and $profit<=100)echo "<script>alert('Nemate dovoljno profita.')</script>";
else if($today==0)echo "<script>alert('Please wait tomorrow.')</script>";
else if($linkDozvola==0)echo "<script>alert('Vas link je neispravan. Link utakmice kopirajte sa www.flashscore.com .')</script>";
else if($kvota<1.01 and $kvota>99.99)echo "<script>alert('Kvota vam nije u opsegu od 1.01 do 99.99 .')</script>";
else if($profit<=100 and $_POST['ulog']<=100) {$result = mysql_query("insert into clanovi_njihovi_parovi set racun='$racun',ip='$ip',datum=curdate(),vrijeme=curtime(),mtime='$mtime',tim1='$tim1',tim2='$tim2', tip='$tip', kvota='$kvota',ulog='$ulog',link='$link';"); }
}
?>[/syntax]
User avatar
leverkusen
Posts: 69
Joined: Sun Nov 18, 2012 10:09 pm
Location: Belgrade
Contact:

Re: Limit to 1 post per day

Post by leverkusen »

jacek wrote:I'm not sure what some of those word mean but you need to do a check using the query I posted above.

Perhaps something like this

[syntax=php]
$result = mysql_query("SELECT COUNT(1) FROM `clanovi_njihovi_parovi` WHERE `racun` = {$racun} AND `datum` > DATE_SUB(NOW(), INTERVAL 1 DAY)");
$row = mysql_fetch_array($result);
$total_posts = $row[0];

if ($total_posts >= 3){
// Show an error message
}else{
// Insert the new post
}
[/syntax]
Just added this to my code and now works perfectly fine except 1 thing, i get message error when i want to post but when there is a post that is posted in the future. I added a post and edited the date to be 30.6.2015 and when i post i get error message. I dont know why, but problem is nearly solved :D
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Limit to 1 post per day

Post by jacek »

That's great news :)

The code only checks for how many posts have been posted since 1 day ago. So it will include any that are in the future as well.

You could change it a bit to ignore future ones by adding a new condition

[syntax=sql]SELECT COUNT(1) FROM `clanovi_njihovi_parovi` WHERE `racun` = {$racun} AND `datum` > DATE_SUB(NOW(), INTERVAL 1 DAY) AND `datum` <= NOW()[/syntax]

Will there ever actually be any in the future though? That sounds impossible to me :P
Image
User avatar
leverkusen
Posts: 69
Joined: Sun Nov 18, 2012 10:09 pm
Location: Belgrade
Contact:

Re: Limit to 1 post per day

Post by leverkusen »

You helped me to solve this problem, thanks alot! :)
Post Reply