Page 1 of 1

Why won't this code work? Looks right to me

Posted: Wed Jun 12, 2002 6:48 am
by oz
I'm trying to greet the user by name when they enter. Trying to use a data base that already stored members names. Name,Email,IP

When they enter I check the ip then check that against the DB list to find there name. Seems like it always displays the first name only. Puzzled??

<?php
$ra=$REMOTE_ADDR;
$thisuser = "Guest";
$connection = mysql_connect ("localhost", "User", "Password");
if ($connection == false){
echo mysql_errno().": ".mysql_error()."<BR>";
exit;
}

$query = "select * from login_info";
$result = mysql_db_query ("lklein", $query );

if ($result){

$numOfRows = mysql_num_rows ($result);
$i = 0;
if ($i <= $numOfRows) {

$name = mysql_result ($result, $i, "fullname");
$email = mysql_result ($result, $i, "email_address");
$ip = mysql_result ($result, $i, "ipaddress");
$i++ ;
if ($ip = $ra) {
$thisuser = $name;

}

}

}
else{
echo mysql_errno().": ".mysql_error()."<BR>";
}

mysql_close ();

echo ("Hello <BR> $thisuser");

?>

Posted: Wed Jun 12, 2002 7:28 am
by twigletmac
Hi, try this code,

Code: Select all

<?php 
$ra = $REMOTE_ADDR; 
$thisuser = 'Guest'; 
@$connection = mysql_connect('localhost', 'User', 'Password') or die(mysql_errno().': '.mysql_error()); 
@mysql_select_db('iklein') or die(mysql_errno().': '.mysql_error());

$query = "SELECT fullname FROM login_info WHERE ipaddress = '".$ra."'"; 
$result = mysql_query($query) or die(mysql_errno().': '.mysql_error()); 

if (mysql_num_rows($result) == 1) &#123;
	$info = mysql_fetch_assoc($result);
	$thisuser = $info&#1111;'fullname'];
&#125;
echo 'Hello '.$thisuser;
mysql_close(); 
?>
It's sort of a condensed version of what you had so will probably be a bit easier to debug.

Mac

Posted: Wed Jun 12, 2002 7:54 am
by oz
Thanks that is shorter. I'm just learning php.
I get this error now with your version.

Warning: Unexpected character in input: ''' (ASCII=92) state=1 in /usr/local/plesk/apache/vhosts/lk-web.com/httpdocs/whois3.php on line 2

Parse error: parse error, unexpected T_VARIABLE in /usr/local/plesk/apache/vhosts/lk-web.com/httpdocs/whois3.php on line 2


Line 2 looks fine to me.
Oz

Posted: Wed Jun 12, 2002 7:59 am
by twigletmac
Line 2 is this line isn't it?

Code: Select all

$ra = $REMOTE_ADDR;
try

Code: Select all

$ra = addslashes($REMOTE_ADDR);
echo $ra;
Mac

Posted: Wed Jun 12, 2002 8:38 am
by gotDNS
If someone is on dial-up, would there IP not change every time?

Just have a login, and then greet them when they login.

Posted: Wed Jun 12, 2002 8:42 am
by twigletmac
Or cookie them and retrieve the value in the cookie and compare it with an id value in your database.

It's really down to you how you decide to go about it.

Mac

Posted: Wed Jun 12, 2002 9:39 am
by mikeq
Is this used on a system where everyone has a static IP address?

Posted: Wed Jun 12, 2002 9:43 am
by Johnm
That is making the assumption that the user has cookies enabled. You will have to test for that if you are going to depend on it. Not everyone allows cookies to be set. For that matter you are also depending on the user not to dump their cookies before their next visit. My experience tells me to avoid depending on the user if at all possible.

Direwolf

Posted: Wed Jun 12, 2002 9:48 am
by oz
Well I still haven't figured out how to use cookies yet. I can't seem to find a decent example that works for me. I've read and searched a lot on that subject.

Oz

Posted: Wed Jun 12, 2002 9:53 am
by twigletmac
That is making the assumption that the user has cookies enabled. You will have to test for that if you are going to depend on it. Not everyone allows cookies to be set.
I only offered the suggestion as a method of identifying a user in order to greet them. It's not reliable but it doesn't have to be. The cookie just being a way to greet someone the next time they arrive at the site and potentially log them in. However, with no cookie they would get greeted as a guest and that's where you stick the login form to allow them to tell you who they are.

I know there's no point excluding someone just because they don't have cookies enabled or depending on anything client side.

Mac

Posted: Thu Jun 13, 2002 8:29 am
by oz
Think I found my main problem, the equal statement

if ($ip = $ra) {

should be

if ($ip == $ra) {

Iknow ips change. I'm just doing this to learn.

Oz