私は、ユーザーが他のユーザーの「良さ」を評価できるPHPスクリプトをプログラムしようとしています。
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
);
改ざんを防ぐために(しようと)過去1時間の間に同じIPからの同じIDのエントリを削除したい
(プロキシ/ルーターによる時折の誤検知は問題ありません-作成者は後で再送信できるため、評価を失っても問題ありませんが、別のIDで登録され、データベース全体を台無しにすることはできません。私がウェブサイトから離れている間):
/* _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;
2つの質問があります:
1)4ではなくIPアドレスの最初の3つの数字だけを比較したい場合、どうすればよいですか? (はい、IPv4ネットワークのA、B、Cタイプについては知っていますが、ここでは関係ありません...)
2)テーブルにインデックスを追加する必要がありますか、それともidとauthorがすでにインデックスに登録されていますか?
ありがとうございました!アレックス