Jump to content

Recommended Posts

shouldnt this add a ip address into my data base on row "login_ip"?

does putting that = sign between the two saying for it to add it?

I appreciate if anyone can help me with this

 

if(empty($row['login_ip'])){ 
	       $row['login_ip'] = $_SERVER['REMOTE_ADDR'];}

 

Are you running this after a SELECT query and before an UPDATE ?

 

if you are

 

if(empty($row['login_ip'])){

$login_ip = $_SERVER['REMOTE_ADDR'];

 

$q = "UPDATE table ................ SET login_ip = '$login_ip'";

}

Are you running this after a SELECT query and before an UPDATE ?

 

if you are

 

if(empty($row['login_ip'])){

$login_ip = $_SERVER['REMOTE_ADDR'];

 

$q = "UPDATE table ................ SET login_ip = '$login_ip'";

}

 

I was just pondering on this last night when editing a login page for a script im building.... cheers for the tip. :)

 

DB

Ya I am running it after a select and before a update

can you please help me understand why this matters? its driving me crazy lol

 

 

<? include_once("connect.php"); ?>

<html>
<body>

<?php
if(isset($_POST['Login'])) {

if(!preg_match('/^[A-Za-z0-9]{5,20}$/',$_POST['Username'])){ // before we fetch anything from the database we want to see if the user name is in the correct format.
         echo "Invalid  Username.";
	 }else{

		 $query = "SELECT password,id,login_ip FROM users WHERE name='".mysql_real_escape_string($_POST['Username'])."'"; 
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result); // Search the database and get the password, id, and login ip that belongs to the name in the username field.

if(empty($row['id'])){
// check if the id exist and it isn't blank.
    echo "Account doesn't exist.";
}else{

	if(md5($_POST['password']) != $row['password']){
		// if the account does exist this is matching the password with the password typed in the password field. notice to read the md5 hash we need to use the md5 function.
            echo "Your password is incorrect."; 
		}else{
			echo "Account available";  


	if(empty($row['login_ip'])){ // checks to see if the login ip has an ip already 
	       $row['login_ip'] = $_SERVER['REMOTE_ADDR'];
	}else{

	$ip_information = explode("-", $row['login_ip']); // if the ip is different from the ip that is on the database it will store it

	if (in_array($_SERVER['REMOTE_ADDR'], $ip_information)) {	
	$row['login_ip'] = $row['login_ip'];
	}else{	
	$row['login_ip'] = $row['login_ip']."-".$_SERVER['REMOTE_ADDR'];
                 		     
	     	
$_SESSION['user_id'] = $row['id'];// this line of code is very important. This saves the user id in the php session so we can use it in the game to display information to the user.
echo "user id is ". $row['id']; 	     
$result = mysql_query("UPDATE users SET userip='".mysql_real_escape_string($_SERVER['REMOTE_ADDR'])."',login_ip='".mysql_real_escape_string($row['login_ip'])."' WHERE id='".mysql_real_escape_string($_SESSION['user_id'])."'")
or die(mysql_error());

// to test that the session saves well we are using the sessions id update the database with the ip information we have received.

header("Location: Sample.php"); // this header redirects me to the Sample.php i made earlier

if(isset($_SESSION['user_id'])) {

// if already logged in.

session_unset();

session_destroy(); 

echo "You have been logged out.";
}
	}
	}
	}	
		      }
	}

	     }

?>

<form id="form1" name="form1" method="post" action=""><center>
  GAME LOGIN
  <br />
  <br />
  Username:
  <input type="text" name="Username" id="Username" />
  <br />
  <br />
Password:
<input type="password" name="password" id="password" />
  <br />
  <br />
  <input type="submit" name="Login" id="Login" value="Login" />
  </center>
</form>

</body>
</html>

Hey scootstah thanks for joining the conversation

 

yah i have this for my connect.php

 

<?php session_start();
ob_start(); 

$mysql_server = "localhost"; // localhost is common on most hosts.

$mysql_user = "shadowing"; //  this is the name of your username of the server.

$mysql_password = "eguitars8"; // the password connected to the username. MAKE IT COMPLEX.

$mysql_database = "spacewars"; // the database name of where to connect to and where the information will be help.

$connection = mysql_connect("$mysql_server","$mysql_user","$mysql_password") or die("Unable to establish a DB connection");

$db = mysql_select_db("$mysql_database") or die("Unable to establish a DB connection");
?>

 

 

 

I am able to get it recording the ip address by moving the } but I dont understand why it fixes it.

 

<? include_once("connect.php"); ?>

<html>
<body>

<?php
if(isset($_POST['Login'])) {

if(!preg_match('/^[A-Za-z0-9]{5,20}$/',$_POST['Username'])){ // before we fetch anything from the database we want to see if the user name is in the correct format.
         echo "Invalid  Username.";
	 }else{

		 $query = "SELECT password,id,login_ip FROM users WHERE name='".mysql_real_escape_string($_POST['Username'])."'"; 
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result); // Search the database and get the password, id, and login ip that belongs to the name in the username field.

if(empty($row['id'])){
// check if the id exist and it isn't blank.
    echo "Account doesn't exist.";
}else{

	if(md5($_POST['password']) != $row['password']){
		// if the account does exist this is matching the password with the password typed in the password field. notice to read the md5 hash we need to use the md5 function.
            echo "Your password is incorrect."; 
		}else{
			echo "Account available";  


	if(empty($row['login_ip'])){ // checks to see if the login ip has an ip already 
	       $row['login_ip'] = $_SERVER['REMOTE_ADDR'];
	}else{

	$ip_information = explode("-", $row['login_ip']); // if the ip is different from the ip that is on the database it will store it

	if (in_array($_SERVER['REMOTE_ADDR'], $ip_information)) {	
	$row['login_ip'] = $row['login_ip'];
	}else{	
	$row['login_ip'] = $row['login_ip']."-".$_SERVER['REMOTE_ADDR'];
}	     
	}    	
$_SESSION['user_id'] = $row['id'];// this line of code is very important. This saves the user id in the php session so we can use it in the game to display information to the user.
echo "user id is ". $row['id']; 	     
$result = mysql_query("UPDATE users SET userip='".mysql_real_escape_string($_SERVER['REMOTE_ADDR'])."',login_ip='".mysql_real_escape_string($row['login_ip'])."' WHERE id='".mysql_real_escape_string($_SESSION['user_id'])."'")
or die(mysql_error());

// to test that the session saves well we are using the sessions id update the database with the ip information we have received.

header("Location: Sample.php"); // this header redirects me to the Sample.php i made earlier

if(isset($_SESSION['user_id'])) {

// if already logged in.

session_unset();

session_destroy(); 

echo "You have been logged out.";
}
}		
}			
}
}		     

?>

<form id="form1" name="form1" method="post" action=""><center>
  GAME LOGIN
  <br />
  <br />
  Username:
  <input type="text" name="Username" id="Username" />
  <br />
  <br />
Password:
<input type="password" name="password" id="password" />
  <br />
  <br />
  <input type="submit" name="Login" id="Login" value="Login" />
  </center>
</form>

</body>
</html>

 

 

Look for this area of the code

$ip_information = explode("-", $row['login_ip']); // if the ip is different from the ip that is on the database it will store it

	if (in_array($_SERVER['REMOTE_ADDR'], $ip_information)) {	
	$row['login_ip'] = $row['login_ip'];
	}else{	
	$row['login_ip'] = $row['login_ip']."-".$_SERVER['REMOTE_ADDR'];
}	     
	}   	

 

I dont understand why having those two }} there instead of at the end of the script like the rest of them matters. This has been driving me crazy all day trying to figure out the brackets

Read the PHP manual section on control structures. If you don't understand them, you're doomed to a life of frustration.

 

http://us.php.net/manual/en/language.control-structures.php

Okay, now that I've cleaned up your code I see the problem. This is your code cleaned up:

if(isset($_POST['Login'])) {	
if(!preg_match('/^[A-Za-z0-9]{5,20}$/',$_POST['Username'])) { // before we fetch anything from the database we want to see if the user name is in the correct format.
         echo "Invalid  Username.";
} else {		 
	$query = "SELECT password,id,login_ip FROM users WHERE name='".mysql_real_escape_string($_POST['Username'])."'"; 
	$result = mysql_query($query) or die(mysql_error());
	$row = mysql_fetch_array($result); // Search the database and get the password, id, and login ip that belongs to the name in the username field.

	if(empty($row['id'])){
		// check if the id exist and it isn't blank.
		echo "Account doesn't exist.";
	} else {		
		if(md5($_POST['password']) != $row['password']){
			// if the account does exist this is matching the password with the password typed in the password field. notice to read the md5 hash we need to use the md5 function.
			echo "Your password is incorrect."; 
		} else {
			echo "Account available"; 

			if(empty($row['login_ip'])){ // checks to see if the login ip has an ip already 
				   $row['login_ip'] = $_SERVER['REMOTE_ADDR'];
			} else {	
				$ip_information = explode("-", $row['login_ip']); // if the ip is different from the ip that is on the database it will store it

				if (in_array($_SERVER['REMOTE_ADDR'], $ip_information)) {	
					$row['login_ip'] = $row['login_ip'];
				} else {	
					$row['login_ip'] = $row['login_ip']."-".$_SERVER['REMOTE_ADDR'];							 
	     	
					$_SESSION['user_id'] = $row['id'];// this line of code is very important. This saves the user id in the php session so we can use it in the game to display information to the user.

					echo "user id is ". $row['id']; 	     

					$result = mysql_query("UPDATE users SET userip='".mysql_real_escape_string($_SERVER['REMOTE_ADDR'])."',login_ip='".mysql_real_escape_string($row['login_ip'])."' WHERE id='".mysql_real_escape_string($_SESSION['user_id'])."'")
					or die(mysql_error());

					// to test that the session saves well we are using the sessions id update the database with the ip information we have received.

					header("Location: Sample.php"); // this header redirects me to the Sample.php i made earlier

					if(isset($_SESSION['user_id'])) {
						// if already logged in.

						session_unset();

						session_destroy(); 

						echo "You have been logged out.";
					}
				}
			}		
		}	
	}
}
}

 

And here is the problem:

if(empty($row['login_ip'])){ // checks to see if the login ip has an ip already 
   $row['login_ip'] = $_SERVER['REMOTE_ADDR'];
} else {	
$ip_information = explode("-", $row['login_ip']); // if the ip is different from the ip that is on the database it will store it

if (in_array($_SERVER['REMOTE_ADDR'], $ip_information)) {	
	$row['login_ip'] = $row['login_ip'];
} else {	
	$row['login_ip'] = $row['login_ip']."-".$_SERVER['REMOTE_ADDR'];			     

	// snipped

	$result = mysql_query("UPDATE users SET userip='".mysql_real_escape_string($_SERVER['REMOTE_ADDR'])."',login_ip='".mysql_real_escape_string($row['login_ip'])."' WHERE id='".mysql_real_escape_string($_SESSION['user_id'])."'")
	or die(mysql_error());

	// snipped
}
}

 

So you are saying if $row['login'] is empty, assign $row['login'] to the users IP. Then you skip the entire process of updating the query, because that is in an else statement. So the only time the update query would ever run was if the $row['logn'] was NOT empty. So you need to move the query outside of that if/else statement.

 

Give this a try:

if(isset($_POST['Login'])) {	
if(!preg_match('/^[A-Za-z0-9]{5,20}$/',$_POST['Username'])) { // before we fetch anything from the database we want to see if the user name is in the correct format.
         echo "Invalid  Username.";
} else {		 
	$query = "SELECT password,id,login_ip FROM users WHERE name='".mysql_real_escape_string($_POST['Username'])."'"; 
	$result = mysql_query($query) or die(mysql_error());
	$row = mysql_fetch_array($result); // Search the database and get the password, id, and login ip that belongs to the name in the username field.

	if(empty($row['id'])){
		// check if the id exist and it isn't blank.
		echo "Account doesn't exist.";
	} else {		
		if(md5($_POST['password']) != $row['password']){
			// if the account does exist this is matching the password with the password typed in the password field. notice to read the md5 hash we need to use the md5 function.
			echo "Your password is incorrect."; 
		} else {
			echo "Account available"; 

			if(empty($row['login_ip'])){ // checks to see if the login ip has an ip already 
				$row['login_ip'] = $_SERVER['REMOTE_ADDR'];
			}	

			$ip_information = explode("-", $row['login_ip']); // if the ip is different from the ip that is on the database it will store it

			if (in_array($_SERVER['REMOTE_ADDR'], $ip_information)) {	
				$row['login_ip'] = $row['login_ip'];
			} else {	
				$row['login_ip'] = $row['login_ip']."-".$_SERVER['REMOTE_ADDR'];
			}					

			$_SESSION['user_id'] = $row['id'];// this line of code is very important. This saves the user id in the php session so we can use it in the game to display information to the user.

			echo "user id is ". $row['id']; 	     

			$result = mysql_query("UPDATE users SET userip='".mysql_real_escape_string($_SERVER['REMOTE_ADDR'])."',login_ip='".mysql_real_escape_string($row['login_ip'])."' WHERE id='".mysql_real_escape_string($_SESSION['user_id'])."'")
			or die(mysql_error());

			// to test that the session saves well we are using the sessions id update the database with the ip information we have received.

			header("Location: Sample.php"); // this header redirects me to the Sample.php i made earlier

			if(isset($_SESSION['user_id'])) {
				// if already logged in.

				session_unset();

				session_destroy(); 

				echo "You have been logged out.";
			}						
		}	
	}
}
}

 

I recommend you look up some common code style practices. Writing clean, organized code is 100x less headache to read and debug, especially when you have a lot of control structures floating around.

 

thanks alot for organizing that for me. that is way easier to read it now. really appreciate the time you took to do that.

 

Your code works perfectly.

why is it that when I remove these lines the ip address stops adding to my database?

 

its everything below the  recording login_ip code

 

                              $_SESSION['user_id'] = $row['id'];// this line of code is very important. This saves the user id in the php session so we can use it in the game to display information to the user.

				echo "user id is ". $row['id']; 

				$result = mysql_query("UPDATE users SET userip='".mysql_real_escape_string($_SERVER['REMOTE_ADDR'])."',login_ip='".mysql_real_escape_string($row['login_ip'])."' WHERE id='".mysql_real_escape_string($_SESSION['user_id'])."'")
				or die(mysql_error());

				// to test that the session saves well we are using the sessions id update the database with the ip information we have received.

				header("Location: Sample.php"); // this header redirects me to the Sample.php i made earlier

				if(isset($_SESSION['user_id'])) {
					// if already logged in.

					session_unset();

					session_destroy();

					echo "You have been logged out.";

			         }

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.