PHP Login System with Admin Features

This forum was created to talk about the PHP Login System with admin features created by jpmaster77 on evolt's website


    Auto login after successfully register

    Share
    avatar
    Admin
    Admin

    Number of posts : 18
    Registration date : 2007-05-12

    Auto login after successfully register

    Post  Admin on Tue Feb 17, 2009 7:12 pm

    Edit constants.php (add one constant):
    Code:
    <$php
    /**
    * Auto Login - this constant determines whether
    * or not to login a new user at the time of
    * registration.
    */
    define("AUTO_LOGIN", true);


    Edit the register() function in session.php:
    Code:
    <$php
      /**
        * register - Gets called when the user has just submitted the
        * registration form. Determines if there were any errors with
        * the entry fields, if so, it records the errors and redirects
        * the user to the form. If none, it attempts to register
        * the new user.
        */
      function register($subuser, $subpass, $subemail){
          global $database, $form, $mailer;  //The database, form and mailer object

      ...

      /* No errors, add the new account to the */
          else{
            if($database->addNewUser($subuser, md5($subpass), $subemail)){
                if(EMAIL_WELCOME){
                  $mailer->sendWelcome($subuser,$subemail,$subpass);
                }
                /*** This is new ************************/
                if(AUTO_LOGIN){
                  /* Username and password correct, register session variables */
                  $this->userinfo  = $database->getUserInfo($subuser);
                  $this->username  = $_SESSION['username'] = $this->userinfo['username'];
                  $this->userid    = $_SESSION['userid']  = $this->generateRandID();
                  $this->userlevel = $this->userinfo['userlevel'];
         
                  /* Insert userid into database and update active users table */
                  $database->updateUserField($this->username, "userid", $this->userid);
                  $database->addActiveUser($this->username, $this->time);
                  $database->removeActiveGuest($_SERVER['REMOTE_ADDR']);
                }
                /*** End of new code ************************/
                return 0;  //New user added succesfully
            }else{
                return 2;  //Registration attempt failed
            }
          }

    That's just the code from the login() function pasted into the register() function before returning 0.

    Edit register.php:
    Code:
    <$php
    /**
    * The user is already logged in, not allowed to register.
    */
    if($session->logged_in && !isset($_SESSION['regsuccess'])){  /*** This is new **********/
      echo "<h1>Registered</h1>";
      echo "<p>We're sorry <b>$session->username</b>, but you've already registered. "
          ."<a href=\"main.php\">Main</a>.</p>";
    }
    /**
    * The user has submitted the registration form and the
    * results have been processed.
    */
    else if(isset($_SESSION['regsuccess'])){
      /* Registration was successful */
      if($_SESSION['regsuccess']){
          echo "<h1>Registered!</h1>";
          echo "<p>Thank you <b>".$_SESSION['reguname']."</b>, your information has been added to the database, "
              ."you are now logged in. <a href=\"main.php\">Main</a>.</p>";  /*** This is new **************/
      }


    That's it.
    avatar
    nichola

    Number of posts : 3
    Registration date : 2009-06-02

    Return a 'we're sorry you've already registered

    Post  nichola on Tue Jun 02, 2009 4:48 am

    I've implemented this script and when I register it says 'We're sorry nichola (or whatever username) you've already registered. When I click on the main link (which goes to main.php) it goes to the logged in page. If I then logout and try to register as someone else and click 'register' I get the 'Success you've registered notice.

    Any ideas I was sure it was working last noght and this moring it isn't and I havent changed anything. I've tested it locally and on my live site.

    Many thanks in advance

      Current date/time is Tue Sep 19, 2017 9:24 am