Solved

Have message come after user tries to login

Posted on 2007-11-28
2
193 Views
Last Modified: 2008-02-01
I would like to have my code where nothing happens until the user tries to login.  RIght now, the code runs as the page loads, therefore outputting the error message.  I don't want the error message to be displayed until a user tries to login.

<?php
session_start();
//Pulling info from login page
if ($_SERVER['REQUEST_METHOD']  == "POST") {
     
      $strUsername = $_POST['strUsername'];
      $strPassword = $_POST['strPassword'];
     
}
 
//Connect to the database
$conn = mysql_connect("localhost", "username", "password")
or die(mysql_error());
mysql_select_db("bank");
 
//Select info from the database
$sql_select = mysql_query("select * from login where strUsername = '" . $strUsername . "' and strPassword = '" . md5($strPassword) . "'")
or die(mysql_error());
 
if(mysql_num_rows($sql_select) != 0)
{
        $_SESSION["username"] = $strUsername;
        header("location: index.php");
}
else
{
        $result = "You are not authorized, please register with us.";
}
 
 
session_write_close();
?>
0
Comment
Question by:pingeyeg
2 Comments
 
LVL 20

Accepted Solution

by:
steelseth12 earned 500 total points
ID: 20370442
<?php
session_start();
//Pulling info from login page

if(isset($_POST['strUsername'])) { // check if form has been submitted

      if ($_SERVER['REQUEST_METHOD']  == "POST") {
             
              $strUsername = $_POST['strUsername'];
              $strPassword = $_POST['strPassword'];
             
      }
      
      //Connect to the database
      $conn = mysql_connect("localhost", "username", "password")
      or die(mysql_error());
      mysql_select_db("bank");
      
      //Select info from the database
      $sql_select = mysql_query("select * from login where strUsername = '" . $strUsername . "' and strPassword = '" . md5($strPassword) . "'")
      or die(mysql_error());
      
      if(mysql_num_rows($sql_select) != 0)
      {
                  $_SESSION["username"] = $strUsername;
                  header("location: index.php");
      }
      else
      {
                  $result = "You are not authorized, please register with us.";
      }

}
 
 
session_write_close();
?>
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 20370459
Awesome!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hosting images 4 28
google map tracking website 5 23
Call to undefined method mysqli_stmt on one live server but not the other 21 50
write screen output to text file 21 47
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question