aetareq Posted September 5, 2015 Share Posted September 5, 2015 I've developed a php script to connect to the mysql, get and store some info of the users. I've heard to write the php in prepared statement to prevent sql injection hacking. I'm quite unfamiliar with this term. So anyone can help me modifying my scripts in procedural prepared statement would be really so much appreciated. here's my code: <?php $ipaddress = ''; if (getenv('HTTP_CLIENT_IP')) $ipaddress = getenv('HTTP_CLIENT_IP'); else if(getenv('HTTP_X_FORWARDED_FOR')) $ipaddress = getenv('HTTP_X_FORWARDED_FOR'); else if(getenv('HTTP_X_FORWARDED')) $ipaddress = getenv('HTTP_X_FORWARDED'); else if(getenv('HTTP_FORWARDED_FOR')) $ipaddress = getenv('HTTP_FORWARDED_FOR'); else if(getenv('HTTP_FORWARDED')) $ipaddress = getenv('HTTP_FORWARDED'); else if(getenv('REMOTE_ADDR')) $ipaddress = getenv('REMOTE_ADDR'); else $ipaddress = 'UNKNOWN'; $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "db_test"; $con = mysqli_connect($servername, $username, $password, $dbname); $sql = "SELECT time FROM userinfo WHERE ipaddress='$ipaddress'"; $result = mysqli_query($con, $sql); if (mysqli_num_rows($result) > 0) { $row = mysqli_fetch_array($result); $rectime = $row['time']; $curtime = date("Y-m-d H:i:s"); $diff = round((strtotime($curtime) - strtotime($rectime))/(60*60)); if ($diff > 0) { $sqli = "INSERT INTO userinfo (id, ipaddress, time) VALUES ('', '$ipaddress', NOW()) ON DUPLICATE KEY UPDATE time = NOW();"; mysqli_multi_query($con, $sqli); echo "welcome again"; } else { echo "welcome"; } } else { $sqli = "INSERT INTO userinfo (id, ipaddress, time) VALUES ('', '$ipaddress', NOW());"; mysqli_multi_query($con, $sqli); echo "welcome"; } mysqli_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/298069-please-modify-my-script-to-the-prepared-statement/ Share on other sites More sharing options...
maxxd Posted September 5, 2015 Share Posted September 5, 2015 Best place to start is always the manual - http://php.net/manual/en/mysqli.prepare.php Basically, you write your SQL, use the mysqli interface to prepare it, then bind the parameters to the placeholders in the query, then run the query. Personally, I've always found PDO easier to deal with for this, so you may want to check that out as well. Quote Link to comment https://forums.phpfreaks.com/topic/298069-please-modify-my-script-to-the-prepared-statement/#findComment-1520317 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.