I want to give to a user his own unique code so that every time he enters my site with his ip (dekstop, mobile and etc) he will receive the same code.
I'v tried using cookies but it's so frustrating and unprofessional that every time I restart my computer/use a different browser/use a different device/use incognito mode it generates me a new code.
I'm kinda new with mysqli, I already tried to replace the "$cookie" lines with mysqli query but then the page didn't load at all..
This is my code, thanks alot! P.S This code works. When I talked about the page that won't load I was talking about the mysqli option that i'v tried and didn't work.
<?php
header("Refresh: 360;");
?>
<?
include "config.php";
if(isset($_COOKIE['my_code'])){
$code = $_COOKIE['my_code'];
}else{
$code = rand(1,9).date('Y').date('m').date('d').date('h').date('i').date('s');
$code = rand_uniqid($code);
setcookie("my_code",$code, 9999999999);
$insert = "insert into cookie_code(user_ip,user_code) values('".getRealIpAddr()."','".$code."');";
@mysql_query($insert);
I want to change the $cookie option with any mysql option that will check if the ip of the user is already in the database, and if it is, so the code from the same row (of the ip) will be given to the user. If there's no ip that matches the ip of the user, he will get a new generated code.
Edit, FAQ:
I know that IP address can't be trusted because it's dynamic, and I take into account that there are people from the same household
that will have the same IP.
I don't want people to register because i'll lose alot of potential users due to their laziness (people don't want to waste time on
registrations). That's going to be a small project that will generate a new code each day (I want to clean the database every 24 hours),
that's why I dont care using ip.. I just need it to hold for 24
hours.
.
Why bother storing the codes if it's only based on ip address? Just make it on the fly
$ip = $_SERVER['REMOTE_ADDR'];
$today = date('Y-m-d');
$ip .= $today;
$code = sha1($ip);