Why won't this code work? Looks right to me
Moderator: General Moderators
Why won't this code work? Looks right to me
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");
?>
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");
?>
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Hi, try this code,
It's sort of a condensed version of what you had so will probably be a bit easier to debug.
Mac
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) {
$info = mysql_fetch_assoc($result);
$thisuser = $infoї'fullname'];
}
echo 'Hello '.$thisuser;
mysql_close();
?>Mac
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
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
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Line 2 is this line isn't it?
try
Mac
Code: Select all
$ra = $REMOTE_ADDR;Code: Select all
$ra = addslashes($REMOTE_ADDR);
echo $ra;- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
- Johnm
- Forum Contributor
- Posts: 344
- Joined: Mon May 13, 2002 12:05 pm
- Location: Michigan, USA
- Contact:
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
Direwolf
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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.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 know there's no point excluding someone just because they don't have cookies enabled or depending on anything client side.
Mac