newbienewbie Posted March 2, 2007 Share Posted March 2, 2007 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. Quote Link to comment Share on other sites More sharing options...
skali Posted March 2, 2007 Share Posted March 2, 2007 On what condition do you not want this code to execute? If you dont want this code to execute on ANY page load then THERE IS NO NEED of this code on this page Quote Link to comment Share on other sites More sharing options...
mbtaylor Posted March 2, 2007 Share Posted March 2, 2007 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). Quote Link to comment Share on other sites More sharing options...
newbienewbie Posted March 2, 2007 Author Share Posted March 2, 2007 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"); } Quote Link to comment Share on other sites More sharing options...
newbienewbie Posted March 2, 2007 Author Share Posted March 2, 2007 On what condition do you not want this code to execute? If you dont want this code to execute on ANY page load then THERE IS NO NEED of this code on this page i want to execute respective code when user click login button. Quote Link to comment Share on other sites More sharing options...
itsmeArry Posted March 2, 2007 Share Posted March 2, 2007 if(isset($_POST['username']) && $_POST['username'] == '') echo "\n Invalid User Name"; this will do Quote Link to comment Share on other sites More sharing options...
mbtaylor Posted March 2, 2007 Share Posted March 2, 2007 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. Quote Link to comment Share on other sites More sharing options...
newbienewbie Posted March 2, 2007 Author Share Posted March 2, 2007 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 Quote Link to comment Share on other sites More sharing options...
itsmeArry Posted March 2, 2007 Share Posted March 2, 2007 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"; Quote Link to comment Share on other sites More sharing options...
newbienewbie Posted March 2, 2007 Author Share Posted March 2, 2007 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"; Quote Link to comment Share on other sites More sharing options...
itsmeArry Posted March 2, 2007 Share Posted March 2, 2007 for what query you are using this while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) and post the complete code... Quote Link to comment Share on other sites More sharing options...
newbienewbie Posted March 2, 2007 Author Share Posted March 2, 2007 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> Quote Link to comment Share on other sites More sharing options...
itsmeArry Posted March 2, 2007 Share Posted March 2, 2007 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> Quote Link to comment Share on other sites More sharing options...
newbienewbie Posted March 6, 2007 Author Share Posted March 6, 2007 no luck, this code if ($usertrace !== '1') { echo "\n Invalid User"; } get executed on form load and as initail value of $usertrace is NULL. it say Invalid user. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.