Jump to content

Date and comparison


xcandiottix

Recommended Posts

I have a comment section on my site. I would like to record the date someone first logs on to my site, add a point to their table id everytime they comment, disallow commenting after 5 points, delete thier ip after 24 hours.

 

So far, I have this:

$ipi = getenv("REMOTE_ADDR");
$sql = "SELECT * FROM iplog WHERE Ip = '$ipi'";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
if (isset($row['Ip'])) {
}
else
{
mysql_query("INSERT INTO iplog (Ip) VALUES ('$ipi')");
}

 

In the INSERT command i'll insert the date but i have not used date functions before in php. Which date format will be the easiest to subtract from to find if their ip has been logged for 24 hours? And what would the code look like that subtracts their log in time to the current time?

 

Thanks for any suggestions.

 

-K.candiotti

Link to comment
Share on other sites

use sessions..

 

<?php
  // top of php files
  include('monitor.php');
?>

<?php
  // monitor.php
  session_start();
  // turn the session into an object to avoid strict notices (unset keys)
  $user = (object) $_SESSION;
  if (isset($temp->logged_in)) {
    if ($temp->logged_in <= (time() - (24 * 60 * 60))) {
      $temp = null;
    }
  } else {
    $temp->logged_in = time();
    $temp->posts = 0;
  }
  $_SESSION = (array) $temp;
?>

 

and then whenever the user posts a post increase $_SESSION['posts'] and check if its greater than 5.. if its greater than five don't continue with the comment..

Link to comment
Share on other sites

I haven't used sessions before, but willing to give it a shot as it seems it will work better. The session will track the individual user that accesses the site no matter which page they are on (because of monitor.php at the top)? And what happens if they X out of their web browser and come back?

Link to comment
Share on other sites

I have a warning:

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/content/x/c/a/xcandiottix/html/chart.php:11) in /home/content/x/c/a/xcandiottix/html/monitor.php on line 3

 

Do you know what could be causing this? I copied your code directly in with out edit.

Link to comment
Share on other sites

OKAY .. final question before I have this thing conquered. For testing I've made a page that echos $_SESSION['posts'] then adds 1 to it then reechos it so i get:

 

01

 

on screen. If i leave this page and come back i would expect to see:

 

12

 

but instead I get 01 again. How to I send the +1 to $_SESSION['posts'] correctly?

 

I tried:

echo $_SESSION['posts'];

$_SESSION['posts']++;

echo $_SESSION['posts'];

 

but that doesn't work...

 

Thanks so much!

Link to comment
Share on other sites

Alrighty, I managed to get the whole thing done with a small brick of code and a 4 column table. Didn't end up using a session though because I'm not that advanced yet ;p

 

The (time() - (24 * 60 * 60)) was a HUGE help tho so thanks so much for that.

 

-K.candiotti

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.