Login, authentication: After entering username+psw nothing happens?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • libra786
    New Member
    • Feb 2007
    • 4

    Login, authentication: After entering username+psw nothing happens?

    I have created a blog and have added a login box which prompts the user for login and id before posting- The username and password have been stored in the database, however when i enter the username and pasword it does not seem to compare the values entered with anything. It jus keeps giving the prompt box to enter details. But if i click cancel, then it tels me the incorrect credentials ave been entered i have tried many ways to solve this problem but cant seem to see where i am gng wrong- the code is below, and some of the code was already provided, Function to authenticate was given -

    And this code is called when the user tries to enter a new posting option from the index page, so before the addentry page displays, the user must login- if any more code is required, let me know-

    Thanks in advance- This is my first ever php program and it was all going ok untill this. thanks

    [PHP] // Function to authenticate
    function authenticated($ username, $password) {

    // Connect to Database Server

    $Hostname = "2006";
    $Username = "anon";
    $Password = "anonpasswo rd";

    //open the connection

    $conn = mysql_connect($ Hostname,$Usern ame, $Password);

    // Choose database

    mysql_select_db ("anon", $conn);

    // Build SQL query to:
    // find row in database with
    // matching $username and $password
    // Execute query

    $query = mysql_query( "SELECT * FROM blog_user WHERE name == '".$username ."' AND password == '".$password."' ;", $conn);


    // IF number of rows is equal to one
    // return true

    if ($query == 1)
    {
    return true;
    }

    // ELSE return false
    else
    {
    return false;
    }

    }


    // Assign username and password from $_SERVER global array

    $username = $_SERVER["PHP_AUTH_U SER"];
    $password = $_SERVER["PHP_AUTH_P W"];



    // Decide whether to show blog entry form, or deny access
    if (!authenticated ($username, $password)) {

    // Credentials either:
    // not sent (1st time script is called)
    // credentials do not match database

    // Display HTTP Authentication Challenge

    header("WWW-Authenticate: Basic Realm=\"Blog Station\"");
    header("HTTP/1.1 401 Unauthorised");

    // Print a message about incorrect credentials

    echo " Incorrect credentials provided, please try again";
    echo ("<a href = \"index.php\ "> Re-Enter");
    exit;
    }

    else {
    // Correct credentials provided, print the blog entry form
    //using the echo as a check
    echo " Authorised";
    }[/PHP]
  • Motoma
    Recognized Expert Specialist
    • Jan 2007
    • 3236

    #2
    Your MySQL query should use '=' rather than '=='

    Where is the code that is getting the username and password? Shouldn't these be taken from a login form? I doubt the $_SERVER[] superglobal will have the correct value.

    Comment

    • libra786
      New Member
      • Feb 2007
      • 4

      #3
      thanks for replying,

      The = does not fix the problem, it still does the same thing -

      Sorry i dont quite understand what you mean by where the code is for username and password. The username and password have been entrered into the table blog_user, i inserted one valid user into the database.

      as for the $_SERVER[] superglobal it was given to us like that, what would be the alternative way of implementing it ?

      Comment

      • libra786
        New Member
        • Feb 2007
        • 4

        #4
        anyone, able to help please?
        Last edited by libra786; Feb 18 '07, 06:46 PM. Reason: hit sumbit by mistake

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          From your reply to motoma I deduct that you are using code that you got from somewhere else. And you have no clue as how to implement and use it.

          Anyway it is not going to work as you showed it. And what you showed is only part of a bigger script.

          So you either display that script or you start to develop a login routine yourself. We can help you with the latter,

          Ronald :cool:

          Comment

          • libra786
            New Member
            • Feb 2007
            • 4

            #6
            Originally posted by ronverdonk
            From your reply to motoma I deduct that you are using code that you got from somewhere else. And you have no clue as how to implement and use it.

            Anyway it is not going to work as you showed it. And what you showed is only part of a bigger script.

            So you either display that script or you start to develop a login routine yourself. We can help you with the latter,

            Ronald :cool:
            Just to clarify i did not copy the code from someone esle or take someone elses code, yes it was given to us by our lecturers, as most people taking the class have little or no knowledge of php and we are only required to write some of the code- Anwyay thanks for your help. I will ask someone esle.

            Comment

            Working...