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
447
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.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      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