SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: Simple Login

  1. #1
    SitePoint Zealot Saj's Avatar
    Join Date
    May 2003
    Location
    USA
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple Login

    Hi.

    I am creating a simple login script which doesn't use MySQL or a text file.

    PHP Code:
    $user $_POST["user"];
    $pass $_POST["pass"];

    if (!
    $submit) {
     echo 
    "<form action=\"admin.php\" method=\"post\">
      <input type=\"text\" size=\"20\" maxlength=\"19\" name=\"user\" />
      <input type=\"password\" size=\"20\" maxlength=\"19\" name=\"pass\" />
      <input type=\"submit\" maxlength=\"19\" value=\"Submit\" name=\"submit\" />
      </form>"
    ;
     }
     else {
      if (
    $user != "admin" || $pass != "password") {
       echo 
    "Error!  Incorrect Username or Password!
       <form action=\"admin.php\" method=\"post\">
    input type=\"text\" size=\"20\" maxlength=\"19\" name=\"user\" />
       <input type=\"password\" size=\"20\" maxlength=\"19\" name=\"pass\" />
       <input type=\"submit\" maxlength=\"19\" value=\"Submit\" name=\"submit\" />
       </form>"
    ;
      }
      else {
       echo 
    "Login successful";
      }
     }
     if (
    $page == "test") {
      echo 
    "This is the test page";
     } 
    I am trying to keep all of this in one file, and also add another page with an option on it. However, when I do so, the form fields come up again because it hasn't been submitted. Is there any way to overcome this?

    Thanks

  2. #2
    SitePoint Zealot
    Join Date
    May 2003
    Location
    Sarasota, FL
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Saj,

    Can you post the code that includes the other page with the option? Seeing that might help us understand what you are trying to do.
    Chris Bloom
    Web Application Developer

  3. #3
    SitePoint Zealot Saj's Avatar
    Join Date
    May 2003
    Location
    USA
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like I had up there...I just forgot the link.

    PHP Code:
      else { 
       echo 
    "Login successful<br /> <a href=\"admin.php?page=test\">Test Page</a>"
      } 

    if (
    $page == "test" ) { 
      echo 
    "This is the test page"


  4. #4
    SitePoint Zealot
    Join Date
    May 2003
    Location
    Sarasota, FL
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    I'm sorry, Saj, but your question still isn't making any sense to me.

    Quote Originally Posted by Saj
    I am trying to keep all of this in one file
    - OK

    Quote Originally Posted by Saj
    and also add another page
    - what page and include how? Like with an include() or require() method or just by echoing text to the screen? Where is the page/text that you are including?

    Quote Originally Posted by Saj
    with an option on it
    - what option?

    A bit more explination would help. Also, walk us through the process and what happens at each step.
    Chris Bloom
    Web Application Developer

  5. #5
    SitePoint Zealot Saj's Avatar
    Join Date
    May 2003
    Location
    USA
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok.

    I just have one file, admin.php. I print a header and footer beforehand, and I want to log in. As you see, I have an !$submit command, which I'm sure means that if the form has not been submitted, then do this. Unfortunatly, when I try to link to another part of this document, admin.php?page=test, it adds the form information on top of what I have specified for the admin.php?page=test. That happens, I think, because I have not submitted the form again before I entered the page.

    I realize this is not the most effective way to do this, but is there a way around this? I'll try to post a test of this...

    Here is a link: http://lycos.co.uk/smscript/admin2.php
    Its basicly the same as here...Username is admin Password is password

  6. #6
    SitePoint Zealot
    Join Date
    May 2003
    Location
    Sarasota, FL
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oooooooohhhhh... Yes, now it all makes sense And you're right, it's not the most effective way to do this (for instance, it looks like I could just go to admin.php?page=test without logging in). But, you already knew that. So, enough of the code police, and onto a fix:

    I would reorganize your code so that all of the $page arguments are processed first. If no $page variable is present then show the login dialog.

    PHP Code:
    $page $_POST["page"];

    if (
    $page == "test" ) {
        echo 
    "This is the test page";

    else if (
    $page == "page1" ) {
        echo 
    "This is page 1";

    else if (
    $page == "page2" ) {
        echo 
    "This is page 2";

    else
        if (!
    $submit) {
            echo 
    "<form action=\"admin.php\" method=\"post\">
            <input type=\"text\" size=\"20\" maxlength=\"19\" name=\"user\" />
            <input type=\"password\" size=\"20\" maxlength=\"19\" name=\"pass\" />
            <input type=\"submit\" maxlength=\"19\" value=\"Submit\" name=\"submit\" />
            </form>"
    ;
        }
        else {
            
    $user $_POST["user"];
            
    $pass $_POST["pass"];

            if (
    $user != "admin" || $pass != "password" ) {
                echo 
    "Error!  Incorrect Username or Password!
                <form action=\"admin.php\" method=\"post\">
                <input type=\"text\" size=\"20\" maxlength=\"19\" name=\"user\" />
                <input type=\"password\" size=\"20\" maxlength=\"19\" name=\"pass\" />
                <input type=\"submit\" maxlength=\"19\" value=\"Submit\" name=\"submit\" />
                </form>"
    ;
            }
            else {
                echo 
    "Login successful<br /> <a href=\"admin.php?page=test\">Test Page</a>";
            }
        }

    This way, one and only one event will occur.
    Chris Bloom
    Web Application Developer

  7. #7
    SitePoint Zealot Saj's Avatar
    Join Date
    May 2003
    Location
    USA
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok! Thanks, that works well for me


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
  •