Login after register

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
msurabbott
Forum Commoner
Posts: 33
Joined: Thu Jan 01, 2009 10:18 pm
Location: Chicago, IL, USA

Login after register

Post by msurabbott »

I am wondering how I can accomplish logging in a user immediately after registration.

Current process goes

Login -> login.php which creates a session object and uses header to redirect the user to /index.php

Register -> register.php which adds the new row to the db and uses header to redirect the user to /index.php

I would like to be able to have it go from register, if the row is added successfully, redirect to login using post (their login info), which will redirect them to /index.php

The only thing I could think of was sending an unsecure request using header header('Location: login.php?username=uname&password=pw').. from the register.php script, but that seems risky...

any ideas?
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: Login after register

Post by novice4eva »

I can think of few things that you could do, the simplest would be to just set the session values as taken in from the register page after successful registration and redirect to the page that you would after a normal login process, that shouldn't cause any problem. Then i think you could use curl too, i haven't used it much myself but i know this can be achieved with curl, post the values from registration to the login page with curl. This might sound stupid since we are doing the same thing ...ahh create a form with hidden fields and then submit it with javascript ok don't do this, just try curl, even better the first option, hope this helped
msurabbott
Forum Commoner
Posts: 33
Joined: Thu Jan 01, 2009 10:18 pm
Location: Chicago, IL, USA

Re: Login after register

Post by msurabbott »

I took your first suggestion but rather than copying the code, I just included login.php in the register script like so..

Code: Select all

if(isset($_POST['submit-registerform'])) {
    try {
        $userDS = new UserDataSource();
 
        // attempt to register the user
        if($userDS->addUser($_POST)) {
            // include the login script, this will log the newly registered
            // user into the system, and send them to the same place all users
            // are sent once authenticated
            include_once('login.php');  
            
            // make sure not to render any more of the page
            return;
        }
    }
    catch (Exception $e) {
        $smarty->assign('error', $e);
    }   
}
Thanks for your help!
Post Reply