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.
Related
I have made a tracking website in php which tracks number of clicks on a specific link - for affiliate links tracking. what I am doing is:
When a user clicks a link provided by my website, he goes to my website which after recording its ip address redirects the user to another address mapped to the link user clicked. A counter increments the number of click after validating ip.
The problem I am facing is that when i compare the number of clicks in my website and that of facebook results, my result is many times more. I don't know what is the cause of that.
My results:
Facebook results:
My question is that why is there a difference? if facebook has some additional checks does someone know what they are? or are they private? or facebook just reduces the number of clicks?
Help would be really appreciated. I am stuck here.
Here is my code to check the visitors ip and increment the click counter:
<?php
require_once "dbdata.php";
if(isset($_GET['linkid']) && !empty($_GET['linkid'])){
$id = $_GET['linkid']; //getting link id to fetch data from database
$ip = $_SERVER['REMOTE_ADDR']; // getting visitors ip address
//database connection
#$db = new mysqli(hostname,username,password,dbname) or die(json_encode(array("status"=>"Can not connect (Database Connection Error)")));
//getting data from table
$query = "select * from links_shared where id = $id ;";
$result_link = $db -> query($query) or die(json_encode(array("status"=>"Error Fetching previous income data")));
$row_link = $result_link-> fetch_assoc();
$link = $row_link['orignal']; //the link to be redirect the user to
header("Location:".$link); //redirected
if($row_link['status'] == "live"){ //status of link should be live
$array_ip = explode(",", $row_link['ip']); //comma sepearted string of ips to array
if(!in_array($ip, $array_ip)){ //check if ip is not already present
$query = "select * from links_deleted where url = '$link' ;"; //getting block list
$result_del = $db -> query($query) or die(json_encode(array("status"=>"Can not select deleted")));
if($result_del -> num_rows <1){ //check if link not in block list
$concat = ",".$ip;
echo $query = "update links_shared set clicks = (clicks + 1), ip = concat(ip,'$concat') where id= $id; ";
$result_update = $db -> query($query) or die(json_encode(array("status"=>"can not update clicks")));
}
}
}
}
?>
Either facebook is invalidating clicks that your script accepts (eg: untrusted IPs, repeated IPs, automatic bot detection...) or more simply facebook only sees clicks from its platform but your script receives all clicks from everywhere.
Of course there could also be a problem with your script itself, but since you don't show it, I can't address that.
I am creating a welcome page for my site that will be displayed to the user if they haven't already been on the website.
I have created a landing page called welcome that will add the users ip to a table in a database when they visit my website.
On my homepage I am trying to make it so that if the users ip is in the database then it won't redirect them to the welcome page and if the ip is not in the database then it will redirect them to the welcome page and on that page their ip will be added to the database so that they do not get redirected next time they visit the site.
Here is my code so far, when I go onto the site it doesn't redirect me to the landing page and I am not sure what the issue is:
<?
$ip = getenv('HTTP_CLIENT_IP')?:
getenv('HTTP_X_FORWARDED_FOR')?:
getenv('HTTP_X_FORWARDED')?:
getenv('HTTP_FORWARDED_FOR')?:
getenv('HTTP_FORWARDED')?:
getenv('REMOTE_ADDR');
$query = "SELECT * FROM landing WHERE ip = $ip";
$result = mysql_query($query);
$num = mysql_num_rows($result);
while($row = mysql_fetch_array($result)){
if($num == '0') {
header("Location: https://tantami.com/welcome/index.php");
} else {
}
}
?>
If enough to check in the database if there's a record of that ip:
$ip = $_SERVER['REMOTE_ADDR'];
$query = "SELECT id FROM landing WHERE ip = '" . mysql_real_escape_string($ip) . "' LIMIT 1";
$result = mysql_query($query);
$num = mysql_num_rows($result);
//if the ip is not in the database make the redirect
if($num == 0) {
header("Location: https://tantami.com/welcome/index.php");
}
There are several issues going on here, in a rough order of importance with regards to your problem they are:
Encase your value in quotes. so SELECT * FROM landing WHERE ip = '$ip' notice the $ip is encased, as is required for strings in a Query.
Please start to use MySQLi rather than the deprecated MySQL. This is more secure, and still supported by both MySQL and PHP. research on StackOverflow how to make the pretty easy transfer.
Your mysql_num_rows is a integer count so should not be in quotes. So set if (mysqli_num_rows($result) == 0 ) .
Immediately after your header() you should add a die or end command such as exit;
PHP should ideally always open with a full <?php declaration.
I'll say it again, use MySQLi.
The $ip should be escaped in this script, specifically, use mysqli_real_escape_string() or even use a REGEX pattern to remove any non valid characters from the $ip, before testing in the SQL query.
Code:
$ip = preg_replace("/[^0-9:.]/","",$ip);
your problem was in your query.
you should add (Apostrophe) before and after of all string in query.
(i also change your select * query yo select count(*))
$ip = getenv('HTTP_CLIENT_IP')?:
getenv('HTTP_X_FORWARDED_FOR')?:
getenv('HTTP_X_FORWARDED')?:
getenv('HTTP_FORWARDED_FOR')?:
getenv('HTTP_FORWARDED')?:
getenv('REMOTE_ADDR');
$query = "SELECT count(*) as nums FROM landing WHERE ip = '$ip'";
$result = mysql_query($query);
$row= mysql_fetch_array($result);
$num=$row['nums'];
if($num == '0') {
header("Location: https://tantami.com/welcome/index.php");
} else {
}
I have created a booking system which uses a clients username from their log in to auto populate a user name field when making a booking. I am not sure of how to get other information like their full name and ID from the database into these fields. Below is the code I have used to verify log in and store their username:
<?php
// Start up your PHP Session
session_start();
// If the user is not logged in send him/her to the login form
if ($_SESSION["Login"] != "YES_client") {
header("Location: login.php");
}
$username = $_SESSION["username"];
?>
I have also implemented the user name in the field using the following code:
echo "<input type='text' name='name' class='form-control' id='FullInputName' value=" . $username . ">"
Is there something simple I am missing? I have tried various methods to display the full data like using $row["Client_ID"] etc but could not get this to work for only the client who is logged into the system. My SQL statement is as follows:
"SELECT * FROM client WHERE Client_username= $username"
I would like to use the Client_ID in the select statement also to make it Unique. I have tried but got various errors.
Any help would be much appreciated!
EDIT
This is the code I have now tried to implement:
$query = "SELECT * FROM client WHERE Client_username='$username'";
echo $query;
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
echo $row['Client_username'];
}
But it is not working correctly - I am receiving this error:
mysql_fetch_array() expects parameter 1 to be resource, boolean given
Starting with your query i think is not correct.
If you are selecting a row and the type is a VARCHAR you need to add single quotes like this:
"SELECT * FROM client WHERE Client_username= '$username'"
Later you can read the results like
(pseudocode) while($row = mysqli_fetch_array) $row['Client_username']
something like that.
Tell me if this works for you
I am capturing the visitors ip address during a form submit using the following.
$users_ip=$_SERVER['REMOTE_ADDR'];
What I would now like to do is see if this ip variable has been used before when submitting a comment, can anyone point me in the right direction?
Maybe a like SQL command?
Assuming you stored client ips in the table named: "ips" then use this:
$connection = mysql_connect($your_db_host, $your_user_account, $your_password);
$mysql_select_db($your_db_name);
$ip = $_SERVER['REMOTE_ADDR'];
$sql = "select 1 from `ips` where `ip`='$ip'";
$sql_resource = mysql_query($sql);
$mysql_close($connection);
if($sql_resource && mysql_num_rows($sql_resource) > 0){
// your logic code if the ip existed in the db
echo 'The ip has been used before';
} else {
// code if the ip not existed in the db
echo 'The ip has not been used before yet';
}
There is a good tutorial explaining how to store IP addresses in MySQL. In short, convert them to long like suggested in this comment, then use simple SELECT statement to find it:
"SELECT COUNT(*) FROM comment WHERE ip = " . ip2long($_SERVER['REMOTE_ADDR'])
Right, I have done a small amount of coding, but have yet to find out how to do the following.
If two users have the same IP address, to mark it through echo.
I wish to make it so that if($user222['current_ip'] (has the same as another user)); { //do action
but I can not find the code any where... does it exsist?
I'd be forever grateful for any help (if you can)
my codes are the following:
`$usersearch = mysql_query ("SELECT * FROM users WHERE online = 'Online'");
$user222 = mysql_fetch_array($usersearch);`
<? if ($user222['reg_ip'] == $user222['current_ip']) {
echo "<font color='green'><b>".$user222['current_ip']."</b></font>";
} else if ($user222['reg_ip'] != $user222['current_ip']) {
echo "<font color='orange'><b>".$user222['current_ip']."</b></font>"; } else //add new code here ?>
EDIT::: I am wanting to find out if two users are registered with the same IP address through SQL. If there is then to echo the reason.
The question is very vague, however the client IP address can be obtained in PHP like this:
$client_ip = $_SERVER["REMOTE_ADDR"];
http://php.net/manual/en/reserved.variables.server.php
You could use cookies if you want to figure out if users are different based upon their browsers. One user could use multiple browsers, though.
As anttix said, you can use "$_SERVER['REMOTE_ADDR']" to obtain the user IP address. You have to register in the MySQL database all the IP addresses, of course.
$result = mysql_query("
SELECT *
FROM users
WHERE
ip = '$_SERVER[REMOTE_ADDR]';
");
while ($row = mysql_fetch_array($result)) {
if ($_SERVER['REMOTE_ADDR'] == $row[ip]) {
// do something
}
}