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 13 of 13
  1. #1
    Regular Coder
    Join Date
    Nov 2007
    Posts
    144
    Thanks
    64
    Thanked 0 Times in 0 Posts

    User Login System

    Hi,

    I've only just recently crossed the bridge from total n00b to "marginally competent" with PHP scripting, so please bear with me.

    I'm working on a user login system and i'm struggling with one of the concepts.

    I have a sidebar which will show a username and password form if there's no session variable set, but if the session variable is set, I want it to say "Welcome back George Bush or whoever the username is.

    Once a user logs in, I want the same page displayed that they were browsing, but with the updated sidebar.

    How do I do that? I don't know what .php file to to point the form action to.

    Any help would be greatly appreciated!

    Thanks!

    Beren

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    Canada, eh?
    Posts
    133
    Thanks
    2
    Thanked 3 Times in 3 Posts
    You would probably track the current page in a hidden form element for each page load, then when the login was a success, simply header("Location: ".$storedpage) them to the last page they were on. You can use $_SERVER['PHP_SELF'] for which page they are on.
    Bored? Might as well play Fantasian Kingdoms: http://www.fantasiankingdoms.com/

    Check out my JavaScript Image Project: http://codingforums.com/showthread.php?t=185231

  • #3
    Regular Coder
    Join Date
    Nov 2007
    Posts
    144
    Thanks
    64
    Thanked 0 Times in 0 Posts
    Fantastic, that's simpler than I thought!

    And how about my sidebar? I've currently got a submit_reg.php which actually does the login and sets session stuff, with the login box on the sidebar, where would you have the form action pointing to?

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Location
    Canada, eh?
    Posts
    133
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Actually, you CAN just point it ANYWHERE the page is located, as long as you include the submit_reg.php page on every page.
    Bored? Might as well play Fantasian Kingdoms: http://www.fantasiankingdoms.com/

    Check out my JavaScript Image Project: http://codingforums.com/showthread.php?t=185231

  • #5
    Regular Coder
    Join Date
    Nov 2007
    Posts
    144
    Thanks
    64
    Thanked 0 Times in 0 Posts
    Could I have the sidebar take care of it all for me, as that is included on every page?

  • #6
    Regular Coder
    Join Date
    Jul 2002
    Location
    Canada, eh?
    Posts
    133
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Sure! As long as it checks for the $_POST data, and sets the session, it can be anywhere. :-)
    Bored? Might as well play Fantasian Kingdoms: http://www.fantasiankingdoms.com/

    Check out my JavaScript Image Project: http://codingforums.com/showthread.php?t=185231

  • #7
    Regular Coder
    Join Date
    Nov 2007
    Posts
    144
    Thanks
    64
    Thanked 0 Times in 0 Posts
    Great, thanks very much for helping me out

  • #8
    Regular Coder
    Join Date
    Jul 2002
    Location
    Canada, eh?
    Posts
    133
    Thanks
    2
    Thanked 3 Times in 3 Posts
    You're welcome. :-)
    Bored? Might as well play Fantasian Kingdoms: http://www.fantasiankingdoms.com/

    Check out my JavaScript Image Project: http://codingforums.com/showthread.php?t=185231

  • #9
    Regular Coder
    Join Date
    Nov 2007
    Posts
    144
    Thanks
    64
    Thanked 0 Times in 0 Posts
    Works! I just did this:

    PHP Code:
       <form name="register" method="post" action="' . $_SERVER['PHP_SELF'] . '"

  • #10
    Regular Coder
    Join Date
    Nov 2007
    Posts
    144
    Thanks
    64
    Thanked 0 Times in 0 Posts
    I'm ALMOST there...

    Upon login it processes one piece of code and logs the user on perfectly, but I want it to also display what's in the else statement.

    PHP Code:


    <div id="container">
      <div id="sidebar1">
    <?php
    include 'database.php';
    include 
    'functions.php';
    if(
    strstr($_SERVER['PHP_SELF'], "register.php")) {
      echo 
    "<p>Please Register</p>";
    }
    elseif(
    $_GET['logoff'] == "yes") {
      
    user_logout();
    }  
    /////////////////// - This code is processed on login - //////////////////////
    elseif(isset($_POST['login'])) {
      
    user_login($_POST['username'], $_POST['password']);
    }
    elseif(!
    is_authed()) 
    {
    echo 
    "LOGIN FORM";
      echo 
    '
        <p>Please log in:</p>
        <form name="register" method="post" action="' 
    $_SERVER['PHP_SELF'] . '">
        <p>Username:<input type="text" name="username" value="'
    ;if(isset($_POST['username'])) { echo $_POST['username']; } echo '"/>Password:
        <input type="password" name="password"/>
        <input type="submit" value="Log in"/>
        <input type="hidden" name="login" value="TRUE"/>
        <br />
        <span class="style1"><a href="register.php">Register</a></span></p>
        </form>'
    ;
    }
    /////////////////// - I Want this part displayed on login - //////////////////////
    else {
      echo 
    '<p>Welcome back ' $_SESSION['username'] . '</p>
            <br>
            <a href="' 
    $_SERVER['PHP_SELF'] . '?logoff=yes">Log Off</a><form><input type="hidden" name="logoff" value=TRUE/></form>';
    }
    ?>
        <p class="style1">&nbsp;</p>
        <!-- end #sidebar1 --></div>
        
          <div id="mainContent">
        <h1 align="right"><a href="index.php">aquabook</a></h1>
    How can I make the welcome message in the else statement be displayed?

    Can I do a GOTO of some description?

  • #11
    Regular Coder
    Join Date
    Jul 2002
    Location
    Canada, eh?
    Posts
    133
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Very simple, don't put the "!is_authed()" bit as an elseif... just put it as an if, then it will check it every time, regardless of if the user is logged in or not.

    PHP Code:
    elseif (!is_authed()) { 
    -->
    PHP Code:
    if (!is_authed()) { 
    Bored? Might as well play Fantasian Kingdoms: http://www.fantasiankingdoms.com/

    Check out my JavaScript Image Project: http://codingforums.com/showthread.php?t=185231

  • #12
    Regular Coder
    Join Date
    Nov 2007
    Posts
    144
    Thanks
    64
    Thanked 0 Times in 0 Posts
    Haha, that simple

    It works perfectly now... woohoo! My first proper script Thanks a lot!

  • #13
    Regular Coder
    Join Date
    Jul 2002
    Location
    Canada, eh?
    Posts
    133
    Thanks
    2
    Thanked 3 Times in 3 Posts
    You're welcome again. :-)
    Bored? Might as well play Fantasian Kingdoms: http://www.fantasiankingdoms.com/

    Check out my JavaScript Image Project: http://codingforums.com/showthread.php?t=185231


  •  

    Posting Permissions

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