?
Solved

Have message come after user tries to login

Posted on 2007-11-28
2
Medium Priority
?
202 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

840 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