Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // In response to http://answers.yahoo.com/question/index?qid=20130513042043AAQO7yN
- // Variables
- $html_header;
- $message;
- $user = (isset( $_POST['user'] ))? trim( $_POST['user'] ) : '';
- $pass = (isset( $_POST['password'] ))? trim( $_POST['password'] ) : '';
- // Check First For POST, no need to use a connection before needed.
- if( strlen($user) > 0 && strlen($pass) > 0){
- // Database Connection ( You should be at least be using MySQLi )
- $conn = mysql_connect( 'localhost', 'username', 'passowrd' );
- if( !$conn ){ // Check For Connection
- if( !mysql_select_db( 'twa051', $conn ) ){
- $message = 'Cannot Select MySQL Database.<br /> Reason: ' . mysql_error($conn);
- }
- }else{
- $message = 'Cannot Connect To MySQL.<br /> Reason: ' . mysql_error($conn);
- }
- // If there's no message, then there's no problems
- if( is_null($message) ){
- // MySQL injection protection (look into PDO)
- $user = mysql_real_escape_string( $user );
- $pass = mysql_real_escape_string( $pass );
- // I'm using 'id' but substitute for whatevery key column you're using
- $query = "SELECT id FROM school_info WHERE username='{$user}' AND password='{$pass}' LIMIT 1";
- $results = mysql_query( $query );
- if( !$results ){
- $message = 'Malformed Query Statement, Or , Internal Error.<b' . 'r />' . mysql_error( $conn );
- }else if( mysql_num_rows( $results ) === 1){ // If it returns a row, then the username and password match.
- $html_header = '<meta http-equiv="refresh" content="0;URL=home.php">';
- header('Location: home.php');
- }else{
- $message = 'Incorrect Username/Password combo.';
- }
- }
- }else if( $_SERVER[ 'REQUEST_METHOD' ] === 'POST'){
- $message = 'Please Enter Both Username and Password.';
- }else{
- $message = 'Please Enter Username and Password.';
- }
- ?><!DOCTYPE html>
- <html>
- <head>
- <title> Form </title>
- <link rel="Stylesheet" type="text/css" href="form.css" />
- <?php if( !is_null($html_header) ){ echo $html_header;} ?>
- </head>
- <body>
- <p>
- <a href="home2.html" >Back to homepage </a>
- </p>
- <img src="logo.png" width="150" height="80" alt="Logo" />
- <br />
- <br />
- <br />
- <br />
- <p>
- <?php echo $message; ?>
- </p>
- <p style="border: 1px solid #333;">
- <form method="post" action="login.php">
- <label>
- Username:
- </label>
- <input type="text" name="user" />
- <br />
- <label>
- Password:
- </label>
- <input type="password" name="password"/>
- <br />
- <input type="submit" value="Login" />
- <input type="reset" value="Reset" />
- </form>
- </p>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement