Jump to content

form load - nothing appears - simple


newbienewbie

Recommended Posts

NEWBIE:

 

i don't want to execute certain lines of code on form load.

 

when i open http://127.0.0.1:8080/testing/login.php in browser. then i don't want to execute following code

if (!$usertrace == '1') 
{
echo "\nInvalid User";
}

what to do for this.

 

And is something wrong with this code

 

if is_null($_REQUEST['username'])
(
echo "\nInvalid User";
)

when i put above in my code form doesn't show any thing.

 

 

Link to comment
https://forums.phpfreaks.com/topic/40829-form-load-nothing-appears-simple/
Share on other sites

You need to watch your variable types. You are looking for a boolean e.g true/false not a string I presume so it should be:

 

if (!$usertrace){
echo ("\nInvalid User");
}

 

writing if (!$usertrace) is the same as saying if $usertrace isn't true do this.

Also try empty for the variable:

 

if (empty($_REQUEST['username'])) {
echo ("\nInvalid User");
}

 

Though it could be because the is_null needs to be in the if brackets (ive not actually used the is_null function I either use empty or test for true/false).

 

 

basically i want to implement 2 things with my login.php page, this page accepts 2 things username and password:

 

1.  when user click login button while there username is emty , then it shld say invalid user

2.  when user eneter wrong user and clicks login buttone, then it shld say invalid user

 

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) 
{
    //echo "\t<tr>\n";
    foreach ($line as $col_value) 
{
	 if ($col_value == $_REQUEST['username']) 
	 {
	  $usertrace=1; 
	  $query = "SELECT pass FROM $my_table WHERE user='$col_value'";
	  $result=mysql_query($query) or die('Query failed: ' . mysql_error());
	  while($row = mysql_fetch_array($result))
		if ($row['pass'] == $_REQUEST['password']) 
		{
    		echo "Welcome!";
		}
		else
		{
			echo "\nInvalid Password";
			}
	 }
}
}    

if (!$usertrace == '1') 
{
echo "\nInvalid User";
}
if (empty($_REQUEST['username'])) 
{
echo ("\nInvalid User");
}

or

 

if (strlen ($_POST['username']) > 0) {
#username has a value
}else{
# it doesnt
}

 

Theres always a dozen ways to do the same thing :)

 

Bear in mind that you should ALWAYS check POST data for potential invalid input before doing anything with it. Otherwise you will get SQL injections, XSS attacks etc.

if(isset($_POST['username']) && $_POST['username'] == '')

    echo "\n Invalid User Name";

 

this will do

 

thanks that work but it doesn't satisfy my 2nd need

 

2.  when user eneter wrong user and clicks login buttone, then it shld say invalid user

 

use this code...


$usertrace = '';
$numRows = mysql_num_rows($result);
if($numRows > 0)
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) 
{
     if ($line['user'] == $_REQUEST['username']) 
    {
       $user = $line['user'];
       $usertrace=1; 
       $query = "SELECT pass FROM $my_table WHERE user='$user'";
       $result=mysql_query($query) or die('Query failed: ' . mysql_error());
       while($row = mysql_fetch_array($result))
       {
            if ($row['pass'] == $_REQUEST['password']) 
{
    		echo "Welcome!";
}
else
{
                    echo "\nInvalid Password";
	}
     }
    }
}
else
echo "\nInvalid User Name";


hi thanks for replying , but else is going out of while. nothing happened when i executed the code, no verification. only 2 boxes with login button displyed

 

 

use this code...


$usertrace = '';
$numRows = mysql_num_rows($result);
if($numRows > 0)
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) 
{
     if ($line['user'] == $_REQUEST['username']) 
    {
       $user = $line['user'];
       $usertrace=1; 
       $query = "SELECT pass FROM $my_table WHERE user='$user'";
       $result=mysql_query($query) or die('Query failed: ' . mysql_error());
       while($row = mysql_fetch_array($result))
       {
            if ($row['pass'] == $_REQUEST['password']) 
{
    		echo "Welcome!";
}
else
{
                    echo "\nInvalid Password";
	}
     }
    }
}
else
echo "\nInvalid User Name";


Thanks for replying again. see following

 

<html>
<body>

<form action="" method="post">
<center>
Username:
<input name="username" type="text" size="10" maxlength="30"/>
Password:
<input name="password" type="password" size="10" maxlength="30" />
<input type="submit" value="Login" />
</form>
</center>

<?php
$mysql_host='localhost';
$mysql_user='xyz';
$mysql_password='xyz';
$my_database='xyz';
$my_table='users';


// Connecting, selecting database
$link = mysql_connect($mysql_host,$mysql_user, $mysql_password)
    or die('Could not connect: ' . mysql_error());

mysql_select_db($my_database) or die('Could not select database');

// Performing SQL query
$query = "SELECT user FROM $my_table";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// Printing results in HTML
//echo "<table>\n";

$usertrace = '';
$numRows = mysql_num_rows($result);
if($numRows > 0)
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) 
{
     if ($line['user'] == $_REQUEST['username']) 
    {
       $user = $line['user'];
       $usertrace=1; 
       $query = "SELECT pass FROM $my_table WHERE user='$user'";
       $result=mysql_query($query) or die('Query failed: ' . mysql_error());
       while($row = mysql_fetch_array($result))
       {
            if ($row['pass'] == $_REQUEST['password']) 
{
    		echo "Welcome!";
}
else
{
                    echo "\nInvalid Password";
	}
     }
    }
}
else
echo "\nInvalid User Name";

//if ($usertrace !== '1')
//{
//	echo "\n Invalid User";
//}

    
//    echo "\t\t<td>$col_value</td>\n";
  //  }
  //  echo "\t</tr>\n";
//}
//echo "</table>\n";

// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($link);
?>

</body>
</html>

try this out

 


<html>
<body>

<form action="" method="post">
<center>
Username:
<input name="username" type="text" size="10" maxlength="30"/>
Password:
<input name="password" type="password" size="10" maxlength="30" />
<input type="submit" value="Login" />
</form>
</center>

<?php
$mysql_host='localhost';
$mysql_user='xyz';
$mysql_password='xyz';
$my_database='xyz';
$my_table='users';


// Connecting, selecting database
$link = mysql_connect($mysql_host,$mysql_user, $mysql_password)
    or die('Could not connect: ' . mysql_error());

mysql_select_db($my_database) or die('Could not select database');

// Performing SQL query
$query = "SELECT user FROM $my_table";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// Printing results in HTML
//echo "<table>\n";

$usertrace = '';
$numRows = mysql_num_rows($result);
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
    if ($line['user'] == $_REQUEST['username'])
    {
       $user = $line['user'];
       $usertrace=1;
       $query = "SELECT pass FROM $my_table WHERE user='$user'";
       $result=mysql_query($query) or die('Query failed: ' . mysql_error());
       while($row = mysql_fetch_array($result))
       {
           if ($row['pass'] == $_REQUEST['password'])
		{
    		echo "Welcome!";
		}
		else
		{
                echo "\nInvalid Password";
	 	}
       }
}
}

if ($usertrace !== '1')
{
echo "\n Invalid User";
}





// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($link);
?>

</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.