SitePoint Sponsor

User Tag List

Results 1 to 15 of 15

Thread: PHP JS and HTML

Hybrid View

  1. #1
    Non-Member leonte's Avatar
    Join Date
    Jul 2004
    Location
    Dubai
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP JS and HTML

    I want to make a nice login system.
    Here is what i had in mind:
    i have a html form - with 2 text inputs (user &pass) and one submit button. I want when the user clicks the button to be directly logged in, if not an error message to appear that the user pass where incorrect - all in the same little box.

    I think i need JS...
    <input type="submit" value="Login" onclick="PHP_FUNCTION_HERE !?!?">
    How can i code this - please give me an example....

    thanks in advance,
    mihai

  2. #2
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would have this as a completely PHP solution.

    The PHP page (say login.php) would look like this (in pseudo-code)

    Code:
      
      IF the submit button was pressed
        IF all the inputs are valid
      	IF this user is a valid user and had supplied the correct password
      	  REDIRECT to successful login page
      	ELSE
      	  SET error message to 'invalid username / password combination'
      	ENDIF
        ELSE
      	SET error message to 'invalid inputs'
        ENDIF
      ENDIF
      
      DISPLAY any error messages
      BUILD the HTML form
    Hope this helps

  3. #3
    Non-Member leonte's Avatar
    Join Date
    Jul 2004
    Location
    Dubai
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is exactly what i need - how do i do this ?

  4. #4

    Join Date
    Oct 2003
    Location
    €uroLand
    Posts
    1,340
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You will need something along these lines

    HTML Code:
    <form method="post" action="login.php">
      <input name="username" type="text" id="username"><br>
      <input name="password" type="password" id="password"><br>
      <input type="submit" name="Submit" value="Submit">
    </form>
    and in login.php something like that
    PHP Code:
    if (isset($_POST['Submit']))
    {
        
    // do your login processing here


  5. #5
    Non-Member leonte's Avatar
    Join Date
    Jul 2004
    Location
    Dubai
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    noooo - i want if the wrong login info is inserted to print directly - if the login is correct to redirect

  6. #6

    Join Date
    Oct 2003
    Location
    €uroLand
    Posts
    1,340
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, simply do it

    Seriously, show the login code you have got so far and we have something to work on.

  7. #7
    Non-Member leonte's Avatar
    Join Date
    Jul 2004
    Location
    Dubai
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <form action="auth.php" method="post">
    <input class="login" type="text"value="nume utilizator" onfocus="if(this.value=='nume utilizator')this.value=''">
    <input class="login" type="password" value="Parola" onfocus="if(this.value=='Parola')this.value=''">
    <h3><input type="submit" value="Login"> <a href="whysignup.php">Se merita?</a></h3>

  8. #8
    Non-Member leonte's Avatar
    Join Date
    Jul 2004
    Location
    Dubai
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i have no code as of yet - i am trying to do this now - the auth code,
    the register is done, but i want a nice login

  9. #9

    Join Date
    Oct 2003
    Location
    €uroLand
    Posts
    1,340
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I dont know your exact table layout nor your overall system design, but something in this should get you started.
    PHP Code:
    if (isset($_POST['text']))
    {
        
    $result=mysql_query('SELECT * FROM users WHERE name="'.$_POST['text'].'" AND password="'.md5($_POST['password']).'"');

        if (
    mysql_num_rows($result)>0)
        {
    // A row with proper data was found

            // Here you need to set a session variable or whatever to indicate the successful logon like
            
    $_SESSION['loggedin']=true;
            
    // for example

            
    header('Location: wherever.php'); // Your redirect
        
    } else echo "Wrong user credentials supplied".

    This isnt the most elegant solution but rather just a basic draft design, but its difficult to get more into detail without knowing more about your system.

  10. #10
    Non-Member leonte's Avatar
    Join Date
    Jul 2004
    Location
    Dubai
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that is the basic deisgn
    to do that checking POST data must be sent - so a redirection. I don`t want that.
    I want when the user clicks on submit - without another page load to show the user if he entered wrong info, else redirection....

    2) is that the best way to check if a user is logged in - with a session var like you did - is it secure ?

  11. #11

    Join Date
    Oct 2003
    Location
    €uroLand
    Posts
    1,340
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by leonte
    that is the basic deisgn
    to do that checking POST data must be sent - so a redirection. I don`t want that.
    I want when the user clicks on submit - without another page load to show the user if he entered wrong info, else redirection....
    Sorry what? The form you provided actually does a POST request.

    Quote Originally Posted by leonte
    2) is that the best way to check if a user is logged in - with a session var like you did - is it secure ?
    Yes, session variables cannot be altered from outside.

  12. #12
    Non-Member leonte's Avatar
    Join Date
    Jul 2004
    Location
    Dubai
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes i know what i did was a post request - but i don`t want that - i want something dynamic please

    thank you for your patience

  13. #13

    Join Date
    Oct 2003
    Location
    €uroLand
    Posts
    1,340
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Simply replace all $_POST references with $_GET.

  14. #14
    Non-Member leonte's Avatar
    Join Date
    Jul 2004
    Location
    Dubai
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    and how do i code the form ? when i click submit A NEW PAGE LOADS

  15. #15

    Join Date
    Oct 2003
    Location
    €uroLand
    Posts
    1,340
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    auth.php will be "loaded" upon submitting this form.
    HTML Code:
    <form action="auth.php" method="get">
    <input class="login" type="text"value="nume utilizator" onfocus="if(this.value=='nume utilizator')this.value=''">
    <input class="login" type="password" value="Parola" onfocus="if(this.value=='Parola')this.value=''">
    <h3><input type="submit" value="Login"> <a href="whysignup.php">Se merita?</a></h3>
    Whether you redirect from auth.php then or simply display the desired content directly from auth.php is up to you.

    This might give you some good additional ideas.


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
  •