I am trying to sanitize $_GET['user'], check if the user exists in the database, and then set it to a variable ($g_user) for use in the rest of the page.
I came up with this and am wondering if it is ok?
Code php:if (isset($_GET['user'])) { $user = filter_input(INPUT_GET, 'user', FILTER_SANITIZE_STRING); $find_user = mysqli_query($link, sprintf("SELECT username FROM users WHERE username = '%s'", mysqli_real_escape_string($link, $user) )); $result = mysqli_num_rows($find_user); if ($result != 1) { $error = 'User not found.'; include 'error.php'; exit(); } else { $g_user = $user; } }
or even this?
Code php:$user = filter_input(INPUT_GET, 'user', FILTER_SANITIZE_STRING); if (empty($user)) { header('Location: /'); exit(); } else { $find_user = mysqli_query($link, sprintf("SELECT username FROM users WHERE username = '%s'", mysqli_real_escape_string($link, $user) )); $result = mysqli_num_rows($find_user); if ($result != 1) { $error = 'User not found.'; include 'error.php'; exit(); } else { $g_user = $user; } }
edit: the reason I ask is because I only just recently learned about filtering and sprintf, so I want to make sure I am doing this the right way.![]()



Reply With Quote





Bookmarks