Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3396776
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:26:53+00:00 2026-05-18T04:26:53+00:00

I’m trying to program a PHP-script, where users can rate the goodness of the

  • 0

I’m trying to program a PHP-script, where users
can rate the “goodness” of the other users:

   create table pref_rep (
           id varchar(32) references pref_users(id) check (id <> author),
           author varchar(32) references pref_users(id),
           author_ip inet,
           good boolean,
           last_rated timestamp default current_timestamp
   );

To (try to) prevent tampering I’d like to delete
entries for the same id coming from the same IP during the course of last hour

(occasional false positives because of proxies/routers are ok – because it’s ok to lose a rating, since the author can re-submit it at any later time; but it’s not ok to have some idiot registered under different id’s and spoiling my whole database while I’m away from the web site):

   /* _author_ip will be $_SERVER['REMOTE_ADDR'] */
   create or replace function pref_update_rep(_id varchar,
           _author varchar, _author_ip inet,
           _good boolean) returns void as $BODY$
           begin
       delete from pref_rep
       where id = _id and
       author_ip = _author_ip and
       age(to_timestamp(last_rated)) < interval '1 hour';

       update pref_rep set
           author    = _author,
           author_ip = _author_ip,
           good      = _good,
           last_rated = current_timestamp
       where id = _id and author = _author;

       if not found then
               insert into pref_rep(id, author, author_ip, good)
               values (_id, _author, _author_ip, _good);
       end if;
           end;
   $BODY$ language plpgsql;

I have 2 questions please:

1) if I’d like to compare just the first 3 numbers of
the IP address instead of the 4, how can I do it?
(yes, I know about the A,B,C types of IPv4 networks, doesn’t matter here…)

2) Do I need to add an index to my table
or are id and author indexed already?

Thank you!
Alex

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T04:26:54+00:00Added an answer on May 18, 2026 at 4:26 am

    1) You can use the & operator:

    postgres=# SELECT '1.2.3.4'::inet & '255.255.255.0'::inet;
     ?column?
    ----------
     1.2.3.0
    (1 row)
    

    So, you can compare

    (author_ip & '255.255.255.0'::inet) = (_author_ip & '255.255.255.0'::inet)
    

    2) I don’t know, are they?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.