IP Logging

tooltiperror

Super Moderator
Reaction score
231
Hello there, ladies and gentlemen. Right now I am building a quote database for an IRC channel I frequently visit.

Question #1
The table for the quotes has a column for the Id (auto_incrementing integer), the quote (text), votes (integer), and ip (char(15)).
i6Y1z.png


My problem is that I only want someone to be able to submit 1 quote per day, so they can't flood it up. I should probably add in captcha validation, but I really don't care that much. So what's the best way of implementing this? Adding a date column doing something like [LJASS]"SELECT * FROM quotes WHERE ip = `used ip`"[/LJASS] and check if all are over a day ago? I feel like there's a better way.

Question #2
I want to have +/- signs so that people vote quotes up or down, and then arrange them by votes. The problem is that someone can keep hitting the + over and over to bring their own quotes up, or stuff like that. I want people to only do + (or -) on a quote once, and never be able to touch that quote again. What's the best way to do this?

Thanks in advance, I appreciate it.
 

phyrex1an

Staff Member and irregular helper
Reaction score
446
1.
Code:
SELECT 1 FROM dual WHERE NOT EXISTS (SELECT * FROM quotes WHERE ip = `used ip` and submitted_timestamp <= `timestamp a day ago`)
Index on ip and submitted_timestamp columns are suggested.

2. Normalize the database, extract the "votes" column to a votes table. (quote_id, user_id or ip address) (bold means that both fields are part of the primary key, you wont need an artificial key) and then join to get to number of votes. Check and insert into the votes table like you do for the quotes table. Later, you might want to unnormalize the database by introducing the votes column to your quotes table again and periodically recount the total votes instead of doing it on the fly but this depends entirely on the performance characteristics of your application. It might end up that the database does a better job optimizing than an unnormalized would do.
 

GetTriggerUnit-

DogEntrepreneur
Reaction score
129
Also, you probably want to get the user's real ip, to avoid proxies. This should get the right one :).
Code:
unction getRealIpAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}
 

JerseyFoo

1/g = g-1
Reaction score
40
Pretty sure the above post is un-necessary, however..

Code:
$ip = $_SERVER['HTTP_CLIENT_IP'] or $_SERVER['HTTP_X_FORWARDED_FOR'] or $_SERVER['REMOTE_ADDR'];
...may work.
 

GetTriggerUnit-

DogEntrepreneur
Reaction score
129
Pretty sure the above post is un-necessary, however..

Code:
$ip = $_SERVER['HTTP_CLIENT_IP'] or $_SERVER['HTTP_X_FORWARDED_FOR'] or $_SERVER['REMOTE_ADDR'];
...may work.
Does not work. Recent versions of PHP need the isset or empty before accessing an array element, perhaps empty calls isset... Otherwise, you'll get an index error. If you really want it in-line, this is the best solution:
Code:
$ip = isset($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] : isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
Pretty sure the above post is un-necessary.
 

Magentix

if (OP.statement == false) postCount++;
Reaction score
107
Pretty sure the above post is un-necessary, however..

Code:
$ip = $_SERVER['HTTP_CLIENT_IP'] or $_SERVER['HTTP_X_FORWARDED_FOR'] or $_SERVER['REMOTE_ADDR'];
...may work.

That would, as GTU said, cause notices if the array key doesn't exist.
You could, however, suppress those notices and use the code below.

Code:
$ip = @$_SERVER['HTTP_CLIENT_IP'] || @$_SERVER['HTTP_X_FORWARDED_FOR'] || @$_SERVER['REMOTE_ADDR'];

For more info, see: http://php.net/manual/en/language.operators.errorcontrol.php
 

JerseyFoo

1/g = g-1
Reaction score
40
PHP:
// Psst.
$var = $herp or $derp; // sort-of-default operator

$var = $herp || $derp; // not a sort-of-default operator

// http://php.net/manual/en/language.operators.logical.php

Going off of memory with the rules of 'or' though, may have changed. I remember it not being as cool as JavaScript's.
 

Magentix

if (OP.statement == false) postCount++;
Reaction score
107
|| has higher precedence than 'or' and is easily interchangeable with binary operators such as 'binary or' (|).

Edit: using a literal 'or' will actually result in failures, seeing as '=' has higher precedence than 'or', but not '||'.
 

JerseyFoo

1/g = g-1
Reaction score
40
Alright, I tested it myself and apologize for the confusion. I just recall using the 'or' operator specifically for a similar purpose where the '||' operator didn't work, perhaps it was a bug in that particular version. Whatever it did or I thought it did; I can't recreate it now.

PHP:
// If you feel like being obnoxious, this will work (tested):

$ip=@$_SERVER['HTTP_CLIENT_IP'] or $ip=@$_SERVER['HTTP_X_FORWARDED_FOR'] or $ip=$_SERVER['REMOTE_ADDR'];

// However I still don't believe any of this proxy nonsense is necessary.
 

Magentix

if (OP.statement == false) postCount++;
Reaction score
107
Alright, I tested it myself and apologize for the confusion. I just recall using the 'or' operator specifically for a similar purpose where the '||' operator didn't work, perhaps it was a bug in that particular version. Whatever it did or I thought it did; I can't recreate it now.

PHP:
// If you feel like being obnoxious, this will work (tested):

$ip=@$_SERVER['HTTP_CLIENT_IP'] or $ip=@$_SERVER['HTTP_X_FORWARDED_FOR'] or $ip=$_SERVER['REMOTE_ADDR'];

// However I still don't believe any of this proxy nonsense is necessary.

If you're going to use 'or' like that, you may as well stick to your original if-statements..
If you want to use short-circuiting, the only sane way to do it is using '||'
 

Artificial

Without Intelligence
Reaction score
326
> If you want to use short-circuiting, the only sane way to do it is using '||'
AFAIK, || in PHP returns a boolean, not the first 'true' value it meets.

PHP:
<?php
$a = 'hello';

$foo = $a || $b || $c;
var_dump($foo, $a);
?>
Output:
Code:
bool(true)
string(5) "hello"
 

Magentix

if (OP.statement == false) postCount++;
Reaction score
107
Seems short circuiting is less lenient in PHP than in JavaScript.
What I said about operator precedence remains true though:
Because the '=' operator precedes 'or', you could still get nasty "bugs".

A sidenote that may be interesting in this thread: Assignments in PHP "return" the truthy or falsy value of what was assigned.
PHP:
// will echo 1
if ($myVar = 1)
{
    echo $myVar;
}

// Fails on any falsy assignment
if ($myVar = array())
{
    echo $myVar;
}
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top