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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
oz
Forum Newbie
Posts: 11
Joined: Sun May 26, 2002 7:15 am
Location: Michigan
Contact:

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

Post 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");

?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
oz
Forum Newbie
Posts: 11
Joined: Sun May 26, 2002 7:15 am
Location: Michigan
Contact:

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

Is this used on a system where everyone has a static IP address?
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post 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
User avatar
oz
Forum Newbie
Posts: 11
Joined: Sun May 26, 2002 7:15 am
Location: Michigan
Contact:

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
oz
Forum Newbie
Posts: 11
Joined: Sun May 26, 2002 7:15 am
Location: Michigan
Contact:

Post 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
Post Reply