PHP & MySQL - Compare IPs that are stored - php

I am trying to create a system to store the last logged on IP, and to compare it to the current IP, then do some functions later on down the road, but currently, I cannot compare them without going to the last else statement. Here's the code.
<?php
$userToPull = $session->userinfo['username'];
$query = "SELECT * FROM users WHERE username='$userToPull'";
$result = mysql_query($query);
while($row = mysql_fetch_row($result)){
$userToShow = $row[25];
$IPtoVerify = $row[26];
}
$lastActivity = RelativeTime($userToShow);
$currIP = $_SERVER['REMOTE_ADDR'];
/*
Shows Partner Stuff
}elseif(!$session->isAdmin()){
echo "<div style='text-align:right;' id='homebox'";
echo "<b>Partner Total:</b> ".$database->getNumMembers()."<br>";
echo $database->num_active_users." partners logged in and ";
echo $database->num_active_guests." guests viewing the site.<br><br>";
echo "</div>";
*/
if(!$IPtoVerify == $currIP){
echo "<div style='text-align:right; background-color: #FAAAB3' id='homebox_partner'";
echo "<b>You are logged on from an unrecognized location.</b><br>";
echo "You will be sent a notification for security purposes.";
echo "<br>This location will automatically be remembered.";
echo "</div><br>";
}elseif($IPtoVerify == $currIP){
echo "<div style='text-align:right;' id='homebox_partner'";
echo "<b>You are logged on from a recognized location.</b><br>";
echo "Your IP is ".$_SERVER['REMOTE_ADDR'];
echo "<br>Your last login was approximately ".$lastActivity;
echo "</div><br>";
}else{
echo "<div style='text-align:right;' id='homebox_partner'";
echo "<b>An error has occurred.</b><br>";
echo "</div><br>";
}
?>
The only thing not working is the if(!$IPtoVerify == $currIP){ if statement.
The IP is stored in the normal fashion, and echo's like: 100.100.100.100. (normal fashion)
Maybe I am not comparing them right, but it has worked for me in the past.

This code doesn't do what you think:
if (!$IPtoVerify == $currIP) {
PHP interprets it as:
if ((!$IPtoVerify) == $currIP) {
You will want to write it as:
if ($IPtoVerify != $currIP) {

Try ($IPtoVerify !== $currIP) instead of (!$IPtoVerify == $currIP)

!$IPtoVerify == $currIP
means
0==$currIP,
because it first validates
`!$IPtoVerify`
which always returns 0 unless $IPtoVerify is 1.
Add additional brackets like
if(!($IPtoVerify == $currIP))...
to solve the issue.

Related

PHP - $_POST['variable'] echos correctly but returns false in if statement

I have a htm file that passes a variable called $team to a php file. The php file echo's 'Fred' just fine on line 3 if that is what the user inputs but an if statement which asks if $_POST["team"] == "Fred" always returns negative. Can anyone help?
<?php
echo $_POST["team"] , "<br />";
if ($_POST["team"] == "Fred"){
echo "You go for " , $_POST["team"];
}
else {
echo "You do NOT go for Fred";
}
?>
Output:
Fred
You do NOT go for Fred
I think $_POST["team"] has spaces. Try this:
<?php
echo $_POST["team"] , "<br />";
if (trim($_POST["team"]) == "Fred"){
echo "You go for " , $_POST["team"];
}
else {
echo "You do NOT go for Fred";
}
?>
Note: this code is not related to the (,) Because you using echo, not merge string
If your $_POST['team'] contains spaces, the comparison will always return false.
Try to use trim() (Documentation here). Trim remove all spaces in your string, so it will reduce chances to fall in errors like these.
<?php
$team = trim($_POST['team']);
echo $team , "<br />";
if ($team == "Fred"){
echo "You go for " , $_POST["team"];
}
else {
echo "You do NOT go for Fred";
}
?>
Let me know if it works =D
you can echo like this because merge string in php use .
if ($_POST["team"] == "Fred"){
echo "You go for " . $_POST["team"];
}

IF within a WHILE (USE OF UNDEFINED CONSTANT)

I have a suspended boolean for each member of staff. when displaying staff members in a table i want to show either the text "SUSPENDED" or "NOT SUSPENDED" rather than 1 or 0.
I keep receiving the error,
Notice: Use of undefined constant Staff_Suspension - assumed 'Staff_Suspension'
Im sure this is simple im fairly new to php, just stuck and dont want to waste anymore time trying to work this out and not get anywhere. help appreciated
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "\t<tr>\n";
echo "\t\t<td> $row[Staff_ID] </td>\n";
echo "\t\t<td> $row[Staff_Forename] </td>\n";
echo "\t\t<td> $row[Staff_Surname] </td>\n";
echo "\t\t<td> $row[Staff_Email] </td>\n";
echo "\t\t<td>";
if ($row[Staff_Suspension] == 1){
echo 'Suspended';
} else if ($row[Staff_Suspension] == 0){
echo 'Not Suspended';
}
echo "</td>\n";
echo "\t\t<td> $row[Staff_Delete_Permissions] </td>\n";
echo "\t</tr>\n";
}
You must quote the indexes. Otherwise, PHP assumes you've defined a constant named Staff_Suspension. If no such constant exists, it then assumes you meant to specify a string literal. Quoting takes away any guess work (and, hence, any notice):
if ($row['Staff_Suspension'] == 1) {
echo 'Suspended';
} elseif ($row['Staff_Suspension'] == 0) {
echo 'Not Suspended';
}
or, simplified:
echo $row['Staff_Suspension'] ? 'Suspended' : 'Not Suspended';
Answer:
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "\t<tr>\n";
echo "\t\t<td>". $row['Staff_ID'] ".</td>\n";
echo "\t\t<td>". $row['Staff_Forename'] ."</td>\n";
echo "\t\t<td>". $row['Staff_Surname']". </td>\n";
echo "\t\t<td>". $row['Staff_Email'] ".</td>\n";
echo "\t\t<td>";
if ($row['Staff_Suspension'] == 1){
echo 'Suspended';
} else if ($row['Staff_Suspension'] == 0){
echo 'Not Suspended';
}
echo "</td>\n";
echo "\t\t<td>". $row['Staff_Delete_Permissions'] ".</td>\n";
echo "\t</tr>\n";
}
Roughly Explained
The use of constants comes mainly from define(); which you do not require a variable for constant values
the reason you are being presented with this error comes from the lines:
if ($row[Staff_Suspension] == 1){
echo 'Suspended';
} else if ($row[Staff_Suspension] == 0){
echo 'Not Suspended';
}
Because you are specifying a key of an array by the name. You should wrap this in quotes, double or single is up to you.
Example of a constant:
define ('Name', 'ConstantValue');
echo Name;
This will output : ConstantValue
now, from reading the above:
http://php.net/manual/en/function.define.php
the link is a rough explination on constant values.
Now for your specific question.
$row[Staff_Suspension] You have defined a constant value, since this is a column name, this should be wrapped in quotes.
If you was specifying from the key number: $row[0]; this is a different story which is irrelevant to your question.

Passing a PHP Value from a link

I am new to PHP and learning. I'm trying to pass a value through a url link but it doesn't seem to work.
The link value I am passing is http://www.mysite.com/index.php?id=f
I want to run a js script if ID not F seen below but right now when I run it. It doesn't do anything:
<?php
$ShowDeskTop = $_GET['id'];
if (isset($ShowDeskTop)){
echo $ShowDeskTop;
if ($ShowDeskTop != "f"){
echo "ShowDeskTop Value is not F";
echo "<script type=\"text/javascript\">";
echo "if (screen.width<800)";
echo "{";
echo "window.location=\"../mobile/index.php\"";
echo "}";
echo "</script>";
};
};
?>
I know this is easy PHP 101 but I can't figure it out. I have tried everything from w3schools to other sites on Google for the answer and having no luck. Could someone please tell me what I am doing wrong?
Thank you!
$ShowDeskTop is not the same as $ShowDesktop variables names are case sensitive!
This is never gonna work since you set the variable AFTER checking if it exist..
The most easy way:
<?php
if (isset($_GET['id'])) {
echo $_GET['id'];
if ($_GET['id'] != 'f') {
?>
<script type="text/javascript">
if (screen.width < 800) {
window.location = "../mobile/index.php";
}
</script>
<?php
}
}
?>
I don't think <> is valid in PHP (it is in VB.NET ..) the is not operator is != or !== (strict/loose comparison).
Also you don't have to close if statements with a ;
This:
if (expr) {
}
Is valid and not this:
if (expr) {
};
I thought about writing != instead of <>.
You have a number of problems including bad variable case (i.e. variables not matching), checking for variables before they exist, etc. You can simply do something like this:
if (!empty($_GET['id'])) { // note I check for $_GET['id'] value here not $ShowDeskTop
$ShowDeskTop = $_GET['id'];
echo $ShowDeskTop; // note I change case here
if ($ShowDeskTop !== "f"){ // note the use of strict comparison operator here
echo "YES, the id doesn't = f";
echo "<script type=\"text/javascript\">";
echo "if (screen.width<800)";
echo "{";
echo "window.location=\"../mobile/index.php\"";
echo "}";
echo "</script>";
} // note the removal of semicolon here it is not needed and is bad coding practice in PHP - this is basically just an empty line of code
} // removed semicolon here as well
Fist thing, you need ; at the end of echo $ShowDesktop
And, what does f mean in if ($ShowDeskTop <> "f"){
use strcmp() instead of <> operator.
Try
if(!strcmp($ShowDeskTop, "f")){
echo "YES, the id doesn't = f";
}
<?php
$ShowDeskTop = $_GET['id']; // assign before checking
if (isset($ShowDeskTop)){
//echo $ShowDeskTop;
if ($ShowDeskTop !== "f"){
echo "YES, the id doesn't = f";
echo "<script type='text/javascript'>";
echo "if (screen.width<800)";
echo "{";
echo "window.location.replace('../mobile/index.php');"; // assuming your path is correct
echo "}";
echo "</script>";
}
}
?>

Comparing 2 exact same strings returns false

I have a variable that is posted through a html form:
$_POST['ref']
And a variable that is pulled from a table in a database:
$row['ref']
i have a basic comparison script to check if they are both the same:
$ref = $_POST['ref'];
$result = mysql_query("SELECT * FROM logbook.job");
if (!$result) {
die("Query to show fields from table failed");
}
$row = mysql_fetch_array($result);
$refdb = $row['ref'];
$refform = $_POST['ref'];
echo $_POST['ref'] ."<br>". $row['ref'] . "<br><br>";
if ($refdb == $refform) {
echo "Yes they are<br><br>";
}
else {
echo "No they are not<br><br>";
}
if (is_string($_POST['ref']))
{
echo "Yes";
} else {
echo "No";
}
echo "<br>";
if (is_string($row['ref']))
{
echo "Yes";
} else {
echo "No";
}
Which outputs:
G2mtxW
G2mtxW
No they are not
Yes
Yes
I echo them both out. Than i ask if they are the same. Then i check whether each is a string.
How come they are not the same? How can i get them to match
Any help would be appreciated
Try using the binary-safe comparison for String:
result = strcmp($str1, $str2);
If the result is 0, then both are the same. Otherwise, they aren't.
One of your strings (probably the one from the DB) might be null-terminated. I've tested the following
$foo = "abc\0";
$bar = "abc";
echo "$foo\n$bar\n";
if($foo == $bar)
echo "Equal.";
else
echo "Not equal."
Output is
abc
abc
Not equal.
Try var_dump-ing both values, check their lengths and inspect them using view-source. They are different in someway.
echo $status_message;
echo "Accepted";
if(strcmp($status_message,"Accepted")==0)
{
echo "equal";
}
else
{
echo "not equal";
}
?>
$row['status'] is a field from table

Show div depending on user logged in

$getquery = mysql_query("SELECT * FROM it_task ORDER BY task_id DESC");
while ($rows = mysql_fetch_array($getquery))
{
$id= $rows['task_id'];
$date=$rows['date'];
$project=$rows['project'];
$topic=$rows['topic'];
$instby=$rows['instby'];
$inst=$rows['inst'];
$dline=$rows['dline'];
$ocome=$rows['ocome'];
$comm=$rows['comm'];
$fin=$rows['fin'];
$dellink="Delete";
$editlink="Edit";
$admin = "MJH";
if(($instby == $username)||($instby == $admin))
{
echo "<div id=\"editcont\">$editlink $dellink</div>";
}
else if($inst == $username)
{
echo "<div id=\"editcont\">$editlink <font face=\"Trebuchet MS, Arial, Helvetica, sans-serif\" size=\"2\">Delete</font></div>"
}
else
{
echo "<div id=\"editcontdisabled\">Edit Delete</div>";
}
.in my code above what i want to do is to show a containing links depending on who is logged in.. however, when the admin logs in he will be able to see the containing $editlink and $dellink.
.I can't seem to find where exactly have i gone wrong.. please help me with this guys! TIA! More Power!
.this is what my code looks like.
if(($instby == $username)||($username == $admin))
$admin = "MJH";
if(($instby == $username)||($instby == $admin)) {
if($inst == $username) {
echo "<div id=\"editcont\">$editlink $dellink</div>";
echo "<div id=\"editcont\">$editlink <font>Delete</font></div>";
}
else {
echo "<div id=\"editcont\">$editlink $dellink</div>";
echo "<div id=\"editcontdisabled\">Edit Delete</div>";
}
}
Thats how i percieved you wanted it, but really, it is very hard to understand whats going on here. Are $inst and $instby meant to be different variables? Also, out of curiousity, why have you double bracketed your if(); statements?
Hope I deciphered it correctly.
PS. I don't think echo();ing html is good practice.

Categories