www.webdeveloper.com
Results 1 to 3 of 3

Thread: User login and Admin login

  1. #1
    Join Date
    Mar 2012
    Location
    India
    Posts
    1

    User login and Admin login

    This is my login.php and my question is whenever any error occured while entering fields it is redirecting to next page. I need to get an error message in the existing page only eg. when username and password are left blanks and if we click submit it is redirecting to next page with an error.. Plz help me out
    <?php

    // Connects to your Database

    mysql_connect("localhost", "root", "") or die(mysql_error());

    mysql_select_db("users") or die(mysql_error());


    //Checks if there is a login cookie

    if(isset($_COOKIE['ID_my_site']))


    //if there is, it logs you in and directes you to the members page

    {
    $username = $_COOKIE['ID_my_site'];

    $pass = $_COOKIE['Key_my_site'];

    $check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());

    while($info = mysql_fetch_array( $check ))

    {

    if ($pass != $info['password'])

    {

    }

    else

    {

    header("Location: members.php");



    }

    }

    }


    //if the login form is submitted

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



    // makes sure they filled it in

    if(!$_POST['username'] | !$_POST['pass']) {
    // here I need a error message which should be display in a same existing page
    $error="Plz fill the fields";
    //header("Location: login.php");
    //die($error);

    }


    // checks it against the database



    if (!get_magic_quotes_gpc()) {

    $_POST['email'] = addslashes($_POST['email']);

    }

    $check = mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error());



    //Gives error if user dosen't exist

    $check2 = mysql_num_rows($check);

    if ($check2 == 0) {

    die('That user does not exist in our database. <a href=add.php>Click Here to Register</a>');
    header("Location: login.php");
    }

    while($info = mysql_fetch_array( $check ))

    {

    $_POST['pass'] = stripslashes($_POST['pass']);

    $info['password'] = stripslashes($info['password']);

    $_POST['pass'] = md5($_POST['pass']);



    //gives error if the password is wrong

    if ($_POST['pass'] != $info['password']) {

    die('Incorrect password, please try again.');

    }
    else

    {


    // if login is ok then we add a cookie

    $_POST['username'] = stripslashes($_POST['username']);

    $hour = time() + 3600;

    setcookie(ID_my_site, $_POST['username'], $hour);

    setcookie(Key_my_site, $_POST['pass'], $hour);



    //then redirect them to the members area

    header("Location: members.php");

    }

    }

    }

    else

    {



    // if they are not logged in

    ?>

    <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">

    <table border="0">

    <tr><td colspan=2><h1>Login</h1></td></tr>

    <tr><td>Username:</td><td>

    <input type="text" name="username" maxlength="40">

    </td></tr>

    <tr><td>Password:</td><td>

    <input type="password" name="pass" maxlength="50">

    </td></tr>

    <tr><td colspan="2" align="right">

    <input type="submit" name="submit" value="Login">

    </td>
    </tr>

    </table>
    <a href="registration.php"> Not A Member </br> Register Here</a>
    </form>

    <?php

    }



    ?>




    <?php
    /*
    // Admin user login functions

    // Check username and password


    // Validate user
    function validateUser($username, $pass) {
    $check = verifyUser($username, md5($pass));

    if ($check) {
    $_SESSION['status'] = 'authorized';

    header('location: index.php');
    } else {
    return 'Please enter a correct username and password';
    }
    }

    function logout() {
    if (isset($_SESSION['status'])) {
    unset($_SESSION['status']);

    // Remove the cookie
    if (isset($_COOKIE[session_name()])) {
    setcookie(session_name(), '', time() - 1000);
    session_destroy();
    }
    }
    }

    function checkMember() {
    session_start();

    if($_SESSION['status'] != 'authorized') {
    header('location: login.php');
    }
    }
    */
    ?>

  2. #2
    Join Date
    Mar 2012
    Location
    Tamilnadu, India
    Posts
    2

    Thumbs up try Ajax

    You iust try the ajax concept....

  3. #3
    Join Date
    Nov 2002
    Posts
    2,632
    Code:
    if(!$_POST['username'] | !$_POST['pass'])
    I would change this to...

    Code:
    if(!isset($_POST['username']) || !isset($_POST['pass']))

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles