I have code check ips addresses if duplicated will redirected to another link but the code is running good when insert value in (ip,user_agent,time
) in table (click
) first time but when i try to check if running with some value it written again on database I need any value (ip,user_agent,time
) write just one time after written to database not written the some value (ip,user_agent,time
)
<?php
$servername = "localhost";
$username = "root";
$password = "rootroot";
$dbname = "data_clicks";
//Create connection to database using mysqli
$conn = new mysqli($servername, $username , $password, $dbname);
//Set variables according to user input on previous form
if(isset($_SERVER['REMOTE_ADDR']) && isset($_SERVER['HTTP_USER_AGENT'])){
$ip = $_SERVER['REMOTE_ADDR'];
$user = $_SERVER['HTTP_USER_AGENT'];
$datetime = date_create()->format('d-m-Y ');
}
//Define the INSERT statement with IGNORE keyword
$sql = "INSERT IGNORE INTO clicks (ip, user_agent, time )
VALUES ('.$ip.', '.$user.', CURRENT_TIMESTAMP)";
if ($conn->query($sql) === false) {
die("Database error:".$conn->error);
}
// link offer
$file = "md5.txt";
$f = fopen($file, 'r');
$line = fgets($f);
fclose($f);
$link="https://facebook.com/?".$line;
//do smth
$contents = file($file, FILE_IGNORE_NEW_LINES);
$first_line = array_shift($contents);
file_put_contents($file, implode("\r\n", $contents));
// Check for success
if ($conn->affected_rows == 0) {
print "<br>Error! <p></p>";
echo "" . $ip .''.$user. " already exists in the DCR";
header("Location: http://www.google.com/");
} else {
//Success and return new id
echo "<br><p></p>Record Added!<p></p>";
header("Location: $link");
}
?>