SitePoint Sponsor

User Tag List

Results 1 to 25 of 25
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2002
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    username and password problem

    Hi how are yo doing? I try to run this program,but it is not working. When I input username and password, the next page should come up " Welcome back $user". However, it doesn't show anything.

    Please help me this script

    My php is 4.2

    Here is codes


    <html>
    <body></body>

    <?php
    if (isset($_POST['formSubmitted']) AND $_POST['formSubmitted'] == "yes"){
    $con = mysql_connect("localhost","itohideo","1234");
    if(!$con) {
    print("Fail DB Connection<br>\n");
    exit;
    }
    if(!mysql_select_db("itohideo")) {
    print("Fail DB Selection<br>\n");
    exit;
    }

    $username=$_POST['username'];
    $password=$_POST['password'];
    $password=md5($password);
    $sql="select * from pk where username='$username' and password='$password'";

    if(!mysql_query($sql))
    {

    echo"Username or Password is incorrect. Please try again.";
    exit();

    }
    else

    {

    $row=mysql_fetch_array($result);
    extract($row);
    echo"welcome back<strong>".$username."</strong>";
    }

    }

    ?>
    Login:
    <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
    <input type="text" name="username" size="23"><br>
    <input type="password" name="password" size="23"><br>
    <input type="submit" value="Send" name="submit">
    <?
    {}
    ?>
    </body>
    </html>


    Thank you very much

  2. #2
    SitePoint Enthusiast Bartimeus's Avatar
    Join Date
    Dec 2000
    Location
    Norway, Trondheim
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should realy hide that database information when you post snippets to theese boards. You can never be carefull enough.

    First:
    - do the forum submit to the correct file? the manual says this about $_SERVER['PHP_SELF']:
    "If PHP is running as a command-line processor, this variable is not available. "

    So since you get a totally clean page that might be a issue, i know that on my php server it would not work so i changed it to the filename just to check. Because if you submitted to this file alteast the loginbox would show.

    Why do you close the body tag at the top before the php script?

    Second:
    where do you post formSubmitted to the page? Unless i am very wrong there is no $_POST variable called forumSubmitted when a form is submitted. Simply check the name and value of your submit button, so it will be:

    if(isset($_POST['submit']) AND $_POST['submit']=="Send")){

    other then that i would suggest using the "insert php code" function so that you can use propper indeting.

    Hope this helps.
    Last edited by Bartimeus; Aug 12, 2002 at 18:37.
    Bjarte S Karlsen
    Roleplayer and webdeveloper with a high nerd factor.

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2001
    Location
    Romania
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think this line:
    PHP Code:
    <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
    should be:

    PHP Code:
    <form action="<?php echo($_SERVER['PHP_SELF']); ?>" method="post">

  4. #4
    SitePoint Evangelist
    Join Date
    Apr 2002
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    still not working

    Thanks you for your help. However, it didn't work. I tried to both ways to run this porgram. When I input username and password, it doesn't come anything.

    Should be " welcome back itohideo"

    Please give another way to run this program

    Thanks

  5. #5
    SitePoint Evangelist
    Join Date
    Apr 2002
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    still not working

    Hi how are you

    YOu are right? Here is a problem

    if (isset($_POST['formSubmitted']) AND $_POST['formSubmitted'] == "yes"){

    chaged like this


    if (isset($_POST['submit']) AND $_POST['submit'] == "Send"){


    but it has warning

    my php editor says
    "Notice: Undefined variable: result in c:\itohideo\ito5.php on line 32

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\itohideo\ito5.php on line 32

    Warning: extract() expects first argument to be an array in c:\itohideo\ito5.php on line 33"

    I just want to show "welcome back username" when I click send, I just don't want to show form(text from) again.

    Please help me

    Thank you for your time

  6. #6
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    These are some of the changes I would make. The most vital one being the "hidden" field in the form. All comments are "// comments"

    PHP Code:
    <html>
    <body></body> // <- WHY DO YOU CLOSE YOUR BODY TAG?

    <?php
    if (isset($_POST['formSubmitted']) AND $_POST['formSubmitted'] == "yes"// <- WHY DO YOU TEST THIS VARIABLE TWICE?
    // WHY NOT: if($_POST['formSubmitted'] == "yes')
    {
        
    $con mysql_connect("localhost","itohideo","1234"); // <- IF THIS IS LEGIT INFORMATION, HIDE IT NEXT TIME.  POSTING IT (PASSWORD, USERNAME) IS A SEVERE SECURITY COMPROMISE
        
    if(!$con)
        {
            print(
    "Fail DB Connection<br>\n");
            exit;
        }
        if(!
    mysql_select_db("itohideo")) // <- SECURITY COMPROMISE IF THIS IS A LEGITIMATE DB
        
    {
            print(
    "Fail DB Selection<br>\n");
            exit;
        }

        
    $username=$_POST['username'];
        
    $password=$_POST['password'];
        
    $password=md5($password);
        
    $sql="SELECT * FROM pk WHERE username='$username' AND password='$password'";

        if(!
    mysql_query($sql))
        {
            echo
    "Username or Password is incorrect. Please try again.";
            exit();
        }
        else
        {
            
    $row=mysql_fetch_array($result);
            
    extract($row);
            echo
    "welcome back<strong>".$username."</strong>";
        }
    }
    ?>

    Login:
    <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
        <input type="text" name="username" size="23"><br>
        <input type="password" name="password" size="23"><br>
        <input type="submit" value="Send" name="submit">
        <input type='hidden' name='formSubmitted' value='yes'> // <- YOU NEED TO CREATE A VARIABLE CALLED "formSubmitted" IN ORDER FOR THE SCRIPT TO TEST FOR A VALID VALUE.  THIS IS SET AS A "hidden" FORM VALUE.
    </form> // <- REMEMBER TO CLOSE YOUR FORM        
    <? // <-
    {} //WHAT IS THIS <- doing here
    ?> // <-
        </body> // <- YOU ALREDY CLOSED YOUR BODY TAG UP TOP
    </html>
    John
    John

  7. #7
    SitePoint Evangelist
    Join Date
    Apr 2002
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    problem

    I try to do your way, but I got sitll warning.

    MY php editor says"

    Notice: Undefined variable: result in c:\itohideo\ito6.php on line 33

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\itohideo\ito6.php on line 33

    Warning: extract() expects first argument to be an array in c:\itohideo\ito6.php on line 34"

    Even though I input different username and password, it comes to "Welcome back username" It should come up Username or Password is incorrect. Please try again. It is somthing wrong.
    Please help me

    thanks

  8. #8
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I found your problem.
    PHP Code:
        if(!mysql_query($sql))
        {
            echo
    "Username or Password is incorrect. Please try again.";
            exit();
        }
        else
        {
            
    $row=mysql_fetch_array($result);
            
    extract($row);
            echo
    "welcome back<strong>".$username."</strong>";
        } 
    You are performing the query but NOT assigning the result to a variable. Then you attempt to retrieve values from the mysql_fetch_array() function with a $result variable that is null.

    Try the following:
    PHP Code:
        $result mysql_query($sql); // <- ADDED
        
    if(!$result// <- CHANGED
        
    {
            echo
    "Username or Password is incorrect. Please try again.";
            exit();
        }
        else
        {
            
    $row=mysql_fetch_array($result);
            
    extract($row);
            echo
    "welcome back<strong>".$username."</strong>";
        } 
    John
    John

  9. #9
    SitePoint Evangelist
    Join Date
    Apr 2002
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    still not working

    PHP John


    hi I actually did your way before you posted.

    but it is not working as I told you
    php edit says"
    Warning: extract() expects first argument to be an array in c:\itohideo\ito6.php on line 36"

    and I input different username and password, it shows "welcome back"

    Please help me this script

  10. #10
    SitePoint Evangelist
    Join Date
    Apr 2002
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    database

    my query is like this


    username itohideo
    password 1234

    that command is insert into pk (username,password) values('itohideo','1234)

    I created my query like this

    is that ok I think that my database is ok

    so I don't know what it is wrong this script

    thanks you very much

  11. #11
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you have a function called "extract()"?

    John

    edit:

    Try this:
    PHP Code:
    $result mysql_query($sql);
        if(!
    $result)
        {
            echo
    "Username or Password is incorrect. Please try again.";
            exit();
        }
        else
        {
            
    $row=mysql_fetch_array($result);
            
    $username $row["username"]; // <- CHANGED
            
    echo"welcome back<strong>".$username."</strong>";
        } 
    Last edited by PHP John; Aug 12, 2002 at 08:48.
    John

  12. #12
    SitePoint Evangelist
    Join Date
    Apr 2002
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    still not working

    Hi i try to run this script so many times, but still not working. I would like to say to everybody "Thank you for helping for this script"

    Thanks

    anyway, i don't know this part actually

    $password=md5($password);

    This part must be wrong.

    And I think that my query must be wrong as well.

    my query is like this


    username itohideo
    password 1234

    that command is insert into pk (username,password) values('itohideo','1234)


    I try to John's way.

    but sill not working. therefore, my query must be wrong.

    Please tell me my query is ok or not

    And How can I imporve this script

    Thanks all

  13. #13
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe there is nothing wrong with you query. Try this;
    PHP Code:
        $username=$_POST['username'];
    echo 
    "$username<br />"// <- ADDED
        
    $password=$_POST['password'];
    echo 
    "$password<br />"// <- ADDED 
    What values are displayed with these echos?

    John
    John

  14. #14
    SitePoint Evangelist
    Join Date
    Apr 2002
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    still not working

    I try to fix and run this script.

    I fixed like this.

    It is not workning now.

    When I input username and password, it doesn't show anything.jsut come back to login


    <html>
    <body></body>

    <?php
    if (isset($_POST['formSubmitted']) AND $_POST['formSubmitted'] == "yes")
    // WHY NOT: if($_POST['formSubmitted'] == "yes')
    {
    $con = mysql_connect("ID","DB","PS");
    if(!$con)
    {
    print("Fail DB Connection<br>\n");
    exit;
    }
    if(!mysql_select_db("itohideo"))
    {
    print("Fail DB Selection<br>\n");
    exit;
    }

    $username=$_POST['username'];
    $password=$_POST['password'];

    $sql="SELECT * FROM pk WHERE username='$username' AND password='$password'";

    $result = mysql_query($sql);
    if(!$result)
    {
    echo"Username or Password is incorrect. Please try again.";
    exit();
    }
    else
    {
    $row=mysql_fetch_array($result);
    $username = $row["username"]; // <- CHANGED
    echo"welcome back<strong>".$username."</strong>";
    }



    }


    ?>

    Login:
    <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
    <input type="text" name="username" size="23"><br>
    <input type="password" name="password" size="23"><br>
    <input type="submit" value="Send" name="submit">
    <input type='hidden' name='formSubmitted' value='yes'>
    </form>
    <?
    {}
    ?>
    </body>
    </html>


    Please help this script

  15. #15
    SitePoint Enthusiast
    Join Date
    Dec 2001
    Location
    Romania
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I tried it the way you posted and it works (though some improvements would be welcome)... Are you sure that you have a record in your table for the pair (username, password) you entered?

    I tried with a table having colums like this:

    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
    username VARCHAR(10)
    password VARCHAR(10)

  16. #16
    SitePoint Evangelist
    Join Date
    Apr 2002
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    not shows "Username or Password is incorrect. Please try again"

    hi how are you doing sleepless?

    I did your way. Actually it works but When I input diffrent username and password, it doesn't show "Username or Password is incorrect. Please try again"

    Just come up "welcome" when I input different username and password. However, when I input right username and password, it shows "welcome $username back"


    I don't know why. Do you have any ideas?

  17. #17
    SitePoint Enthusiast
    Join Date
    Dec 2001
    Location
    Romania
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    With (!$result) you can check only whether your sql query failed...


    Try this to check for incorrect username/password:

    PHP Code:
    if(mysql_num_rows($result)==0)
        {
            echo
    "Username or Password is incorrect. Please try again.";
            exit();} 
    Last edited by sleepless; Aug 15, 2002 at 09:55.

  18. #18
    SitePoint Evangelist
    Join Date
    Apr 2002
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    it works last QUA

    Hi how are you doing?

    Wo it works sleepless? You are the best!

    Thank you very much. i just want to ask you one QUA.

    When I input right username and password, it comes with text boxies and button. Therefore, when I click Send button, it should come up only "Welcome back $username".

    How can you do that.

    Please explain to me

    Thanks a lot

  19. #19
    SitePoint Evangelist
    Join Date
    Apr 2002
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    again

    sorry again sleepless


    First QUA
    What I want to do is, if a user inputs right username
    and right password, it goes to my HP. Lets say
    "yahoo.com". How can I make a link for that.

    Second QUA
    I jsut want to show "welcome back $username" to next
    page. not come with text boxies and Send button.

    Could tell me how to do it

    Thanks sleepless

  20. #20
    SitePoint Evangelist
    Join Date
    Apr 2002
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help

    sleepless

    help me thanks

  21. #21
    SitePoint Enthusiast
    Join Date
    Dec 2001
    Location
    Romania
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi! Try this, I think this should be ok.

    PHP Code:
    <html>
    <body>

    <?php
    if (isset($_POST['formSubmitted']) AND $_POST['formSubmitted'] == "yes"
    // WHY NOT: if($_POST['formSubmitted'] == "yes')
    {
    $con mysql_connect("localhost","user","pass"); 
    if(!
    $con)
        {
        print(
    "Fail DB Connection<br>\n");
        exit;
    }

    if(!
    mysql_select_db("itohideo")) 
        {
        print(
    "Fail DB Selection<br>\n");
        exit;
    }

    $username=$_POST['username'];
    $password=$_POST['password'];

    $sql="SELECT * FROM pk WHERE username='$username' AND password='$password'";

    $result mysql_query($sql);
    if(
    mysql_num_rows($result)==0)
        {
        echo
    "Username or Password is incorrect. Please try again.";
    }
    else
    {
        
    $row=mysql_fetch_array($result);
        
    $username $row["username"]; // <- CHANGED
        
    echo ("welcome back &nbsp;<strong>".$username."</strong><br/>");
        echo (
    "<a href=\"www.yourpage.com\">Click here to go to your home page </a>");
        exit();
    }

    ?>

    Login:
    <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
    Name: <input type="text" name="username" size="23"><br>
    Pass: <input type="password" name="password" size="23"><br>
    <input type="submit" value="Send" name="submit">
    <input type='hidden' name='formSubmitted' value='yes'> 
    </form>

    </body>
    </html>

  22. #22
    SitePoint Evangelist
    Join Date
    Apr 2002
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    thanks

    thanks sleepless

    you are good really.

    I am happy now Last QUA

    I would like to know when I click submit button, it goes to my home page. Before I asked you to how to make a link to next page. but this time when one clicks, it goes to my home page.

    DO you know how to do it

    Thanks again

  23. #23
    SitePoint Enthusiast
    Join Date
    Dec 2001
    Location
    Romania
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you mean that after you click the submit button, you want to go to straight to your homepage, without having to click the link?

  24. #24
    SitePoint Enthusiast
    Join Date
    Dec 2001
    Location
    Romania
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If that's what you mean, then try the code below... and remember, I'm not at all good, but I also try to learn from the Sitepoint's great guys!

    PHP Code:

    <html>
    <body>
    <?php
    if (isset($_POST['formSubmitted']) AND $_POST['formSubmitted'] == "yes"
    // WHY NOT: if($_POST['formSubmitted'] == "yes')
    {
    $con mysql_connect("localhost","user","pass"); 
    if(!
    $con)
        {
        print(
    "Fail DB Connection<br>\n");
        exit;
    }

    if(!
    mysql_select_db("itohideo")) 
        {
        print(
    "Fail DB Selection<br>\n");
        exit;
    }

    $username=$_POST['username'];
    $password=$_POST['password'];

    $sql="SELECT * FROM pk WHERE username='$username' AND password='$password'";

    $result mysql_query($sql);
    if(
    mysql_num_rows($result)==0)
        {
        echo
    "Username or Password is incorrect. Please try again.";
    }
    else
    {
        
    $row=mysql_fetch_array($result);
        
    $username $row["username"]; // <- CHANGED
    //    echo ("welcome back &nbsp;<strong>".$username."</strong><br/>");
        
    $yourpage="http://www.yourpage.com";
        
    header("Location: $yourpage");
        exit();
    }

    ?>

    Login:
    <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
    Name: <input type="text" name="username" size="23"><br>
    Pass: <input type="password" name="password" size="23"><br>
    <input type="submit" value="Send" name="submit">
    <input type="hidden" name="formSubmitted" value="yes"> 
    </form>

    </body>
    </html>

  25. #25
    SitePoint Evangelist
    Join Date
    Apr 2002
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks

    Thanks sleepless. You helped me a lot

    Really thanks to you

    Thanks again


Bookmarks

Posting Permissions

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