Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2010
    Location
    The Netherlands
    Posts
    53
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Question In-file login script

    Hello,

    I try to write a login script that can be part of the file.
    Like this:
    {index.php
    {index page}
    {login}
    {restricted page}
    }

    So I wrote a script but used the GOTO command.
    PHP Code:
    <?

    $user 
    'admin';
    $pass 'pass';

    $userinput $_POST['userinput'];
    $passinput $_POST['passinput'];

    $error 'Wrong login, try again.'

    echo 
    "<html><head><title>Login script</title></head><body>
    <form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>
            Username:<br/>
                <input type='text' name='userinput'/><br/>
            Password:<br/>
                <input type='password' name='passinput'/><br/>
            <input type='submit' value='Login' />  
    </form></body></html>"
    ;

    if(
    $user==$userinput)
        {
          if(
    $pass==$passinput)
          {
    goto page;}
         else {echo 
    $error;}
        }
        else {echo 
    $error;}

    page:
    echo 
    "<p>This content is visable after logging in</p>";
    ?>
    I don't know if this works, but I want this to work on lower version PHP servers.
    So I adjusted the script:
    PHP Code:
    <?

    $user 
    'admin';
    $pass 'pass';

    $userinput $_POST['userinput'];
    $passinput $_POST['passinput'];

    $error 'Wrong login, try again.'

    echo 
    "<html><head><title>Login script</title></head><body>
    <form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>
            Username:<br/>
                <input type='text' name='userinput'/><br/>
            Password:<br/>
                <input type='password' name='passinput'/><br/>
            <input type='submit' value='Login' />  
    </form></body></html>"
    ;

    if(
    $user==$userinput)
        {
          if(
    $pass==$passinput)
          {echo 
    "<p>This content is visable after logging in</p>";}
         else {echo 
    $error;}
        }
        else {echo 
    $error;}

    ?>
    But this is resulting in a white page.

    What am I doing wrong?

    [NOTE]The scripts I write are as small and simple as is possible by me.[/NOTE]
    Last edited by Pcfr43k; 01-27-2010 at 05:17 PM.

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    try this;

    PHP Code:
    <?
    $user 
    'admin';
    $pass 'pass';

    $userinput $_POST['userinput'];
    $passinput $_POST['passinput'];

    $LoginFrm "<html><head><title>Login script</title></head><body>
    <form action='" 
    $_SERVER['PHP_SELF'] . "' method='post'>
            Username:<br/>
                <input type='text' name='userinput'/><br/>
            Password:<br/>
                <input type='password' name='passinput'/><br/>
            <input type='submit' value='Login' />  
    </form></body></html>"
    ;

    if(isSet(
    $userinput)){
        if(
    $user == $userinput && $pass == $passinput){
            echo 
    "<p>This content is visable after logging in</p>";
        } else {
            echo 
    "<p>Wrong login, try again.</p>";
            echo 
    $LoginFrm;
        }
    } else {
        echo 
    $LoginFrm;
    }
    ?>

  • Users who have thanked angst for this post:

    Pcfr43k (01-27-2010)

  • #3
    New Coder
    Join Date
    Jan 2010
    Location
    The Netherlands
    Posts
    53
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Yes! That works, thanks.

    But what I want to try is to keep the login as one block.
    Thats why I like PHP 5.3 and up, because of the GOTO command.

    Is there some way to achieve this in PHP 5.2 and lower?

    So like this:
    {index.php
    {login}
    {index content}
    }

    Instead of now:
    {index.php
    {login start
    {index content}
    login end}
    }
    Last edited by Pcfr43k; 01-27-2010 at 02:57 PM.

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,548
    Thanks
    8
    Thanked 1,094 Times in 1,085 Posts
    Use PHP sessions for log-in, and then once they log in, you can check
    the login on any other page. They stay logged-in until they log out or
    until they close their browser.


  •  

    Tags for this Thread

    Posting Permissions

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