Solved

Have message come after user tries to login

Posted on 2007-11-28
2
194 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
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.

828 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