简体   繁体   中英

Display dynamic PHP content based on IP address

In my MySQL database you login via the game and then type your username in on the site and see your stats. This is my information snippet from my API.

elseif ($_GET['task'] == 'login') {
        $get_user = $_GET['user'];
        $get_db = 'engine';
        $result = mysql_query("SELECT * FROM $get_db WHERE name = '" . mysql_real_escape_string($get_user) . "'", $link);
            while($data = mysql_fetch_array($result)) {
                echo '{"task":"login","password":"'; echo $data['hash'];
                echo '","lastip":"'; echo $data['lastip'];
                echo '","timestamp":"'; echo $data['logindate'];
                echo '"}';
            }
    }

I am trying to add a line of text under the search textbox on the homepage that will pop up if your IP recently logged into a server. It'll say "Hey, aren't you username?"

What type of code would be required to do this? I would need to call upon their IP, and search the database for all users who have authenticated with it, and then make sure to display the latest one based on their timestamp.

You can use both $_SERVER['REMOTE_ADDR'] and $_SERVER['HTTP_X_FORWARDED_FOR'] to get the public IP and sometimes (with the second case) the private IP.

By the way, notice that old mysql_*() functions are deprecated as of PHP 5.5 . You should use the mysqli or PDO_MySQL extensions.

$_SERVER['REMOTE_ADDR'] will give you the IP address of the person viewing the page.

From there you just need to compare it to the lastip field in your query.

$userIP = $_SERVER['REMOTE_ADDR'];
$query = "SELECT * FROM ".$get_db." WHERE lastip = ".$userIP." ORDER BY logindate DESC LIMIT 1";

Or something along those lines.

You could use the $_SERVER['REMOTE_ADDR'] , which contains the IP of the viewer. Then you could run

SELECT name FROM 'users' where users.known_ips LIKE '%$ip%' AND users.loggedin = 0 ORDER BY lastlogin DESC LIMIT 1;

('%' represents 0 or more characters. It's a wildcard .) And then you could use

mysqli_stmt_bind_results($ipquery,$name);
mysqli_stmt_fetch($ipquery);
# Now $name = first username found

This method will get their name if they have ever used that computer to log in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM