connect_error) { echo '

MySQL Server is not connected

'; } else { /** Loads local database using connection variable. */ $db = mysqli_select_db($con, "ihatemyfriends"); /** Uses the question card ID to get all of the matches, then it gets the content of those cards. */ $results = mysqli_query($con, "SELECT id, content FROM answers WHERE id IN (SELECT answerID FROM matches WHERE questionID = {$questionCardID}) ORDER BY RAND( )"); /** Choses a random number based how result size. */ /** Results - 1 so there is no overflow. */ //$randomIndex = random_int(0, mysqli_num_rows($results)-1); $total_rows = mysqli_num_rows($results)-1; $randomIndex = shell_exec('echo $((RANDOM%'.(string)$total_rows.'+1))'); /** Displays the content. */ $counter = 0; while ($row = $results->fetch_assoc()){ if ($counter == $randomIndex){ echo $row['content']."."; $answerID = $row['id']; } $counter++; } /** ALGO CREDIT - https://www.codegrepper.com/code-examples/php/get+ip+location+php */ /** Gets Client IP. */ $clientIP = $_SERVER['HTTP_X_REAL_IP']; /** Gets location based off ip. */ $details = json_decode(file_get_contents("http://ipwho.is/{$clientIP}")); /** Gets the country of the scan. */ //$country = $details->country; /** Indicates that testing mode is enabled. */ if($test_mode){ echo ""; } /** Checks to see if the country variable has been set. */ elseif(isset($country)){ /** Saves country, state, city, browser language, and type of device to be stored in DB. */ $state = $details->region_code; $city = $details->city; $browserLanguage = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 5); $deviceBrowserInfo = $_SERVER['HTTP_USER_AGENT']; /** Code that extracts the device name from the returned string. */ $subString_start = strpos($deviceBrowserInfo, "("); $subString_start += strlen("("); $size = strpos($deviceBrowserInfo, ";", $subString_start) - $subString_start; $device = substr($deviceBrowserInfo, $subString_start, $size); /** Query to insert values into scanInfo table */ $insertScanInfo = mysqli_query($con, "INSERT INTO scanInfo(questionID,answerID,country,state,city,browserLanguage,device) VALUES({$questionCardID},{$answerID},\"{$country}\",\"{$state}\",\"{$city}\",\"$browserLanguage\",\"$device\")"); } /** If the location could not be determined. */ else{ /** Holds the text to indicate no answer. */ $no_answer = "NA"; $browserLanguage = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 5); /** Code that extracts the device name from the returned string. */ $deviceBrowserInfo = $_SERVER['HTTP_USER_AGENT']; $subString_start = strpos($deviceBrowserInfo, "("); $subString_start += strlen("("); $size = strpos($deviceBrowserInfo, ";", $subString_start) - $subString_start; $device = substr($deviceBrowserInfo, $subString_start, $size); /** Inserts only the question ID and answer ID. Everything else is NA. */ $insertScanInfo = mysqli_query($con, "INSERT INTO scanInfo(questionID,answerID,country,state,city,browserLanguage,device) VALUES({$questionCardID},{$answerID},\"{$no_answer}\",\"{$no_answer}\",\"{$no_answer}\",\"{$browserLanguage}\",\"{$device}\")"); } } /** Closes the connection. */ $con->close(); # Click here for pricing #https://ipwhois.io/pricing ?>