Jump to content


Photo

HTML Forms in Php.


  • Please log in to reply
1 reply to this topic

#1 Drezard

Drezard
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts

Posted 27 May 2006 - 03:08 AM

Hi, In this script it shows the HTML form whether or not the person is logged in. What i want it to do is when the person is logged in, not show the form and just say the message. Thanks, Daniel

<?php
session_start();
if (isset($_POST['submit'])) {  // check to see if the forum has been submitted
    //  where is tasys 'submit', use the name of the submit button on the form
   include('connect.php');
   // get form input
    // check to make sure it's all there
    // escape input values for greater safety
    $user = empty($_POST['user']) ? die ("ERROR: Enter a Username") : mysql_escape_string($_POST['user']);
    $pass = empty($_POST['pass']) ? die ("ERROR: Enter a Password") : mysql_escape_string($_POST['pass']);
   $sql="SELECT * FROM users WHERE user='$user' and pass='$pass'";
   $result=mysql_query($sql);
   // Mysql_num_row is counting table rows
   $count=mysql_num_rows($result);
   // If result matched $user and $pass, table row must be 1 row
   if($count==1){
      // Register $user, $pass and redirect to file "login_success.php" and make cookie to save user data
$_SESSION['userinfo'] = mysql_fetch_array($result);
?>

<script language='javascript'>
document.location.href = 'login_sucess.php';
</script>

<?php

   }
   else {
      echo "Wrong Username or Password";
   }
}

    if (!isset($_POST['submit'])) {                                  // If the form hasn't been submitted.

?>
              <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
                Username: 
                <input type="text" name="user">
                Password: 
                <input type="text" name="pass">
                <input type="submit" name="submit">
              </form>
              <?php
    }
    if (isset($_COOKIE['user'])) {                            // If the user is already logged in.
echo "You are already logged in.";                
}
?>


#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 27 May 2006 - 04:29 AM

Change this:
<?php
    if (!isset($_POST['submit'])) {                                  // If the form hasn't been submitted.

?>
              <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
                Username:
                <input type="text" name="user">
                Password:
                <input type="text" name="pass">
                <input type="submit" name="submit">
              </form>
              <?php
    }
    if (isset($_COOKIE['user'])) {                            // If the user is already logged in.
echo "You are already logged in.";                
}?>
to
<?php
    if (!isset($_POST['submit']) && !isset($_COOKIE['user'])) {  // If the form hasn't been submitted and the user isn't logged in

?>
              <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
                Username:
                <input type="text" name="user">
                Password:
                <input type="text" name="pass">
                <input type="submit" name="submit">
              </form>
              <?php
    }
    if (isset($_COOKIE['user'])) {                            // If the user is already logged in.
echo "You are already logged in.";                
}?>

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users