Search code examples
cakephpauthenticationforumsmf

How to integrate auto login SMF forum in CakePHP 2.x?


I am going to integrate smf forum v.2.0.9 in my cakephp 2.x. I am using SSI.php

I have a users table in cakephp and another table name smf_members. I have successfully registered by using CakePhp code.

I have also successfully login in forum but page not redirected to my login page.

My code:

login.ctp

$_SESSION['login_url']='http://mydomainname.com/login';
$_SESSION['logout_url']='http://mydomainname.com/logout';

<form accept-charset="UTF-8" action="http://mydomainname.com/forum/index.php?action=login2" method="post">
      <input type="text" name="user" id="user" placeholder="Emailid" />
      <input type="password" id="passwrd" name="passwrd" placeholder="Password" />
      <input type="password" id="passwrd" name="passwrd" placeholder="Password" />
</form>

Above code successfully login. But page not redirect it stay in the forum index.php page. After login I need redirect page to http://mydomainname.com/login

This file is working if i am writing in core

Code:

login.php

<?php
include('forum/SSI.php');
$_SESSION['login_url']='http://mydomainname.com/login';
$_SESSION['logout_url']='http://mydomainname.com/logout';

/*echo "<pre>";
print_r($_SESSION); //exit;
echo "</pre>"; */
ssi_login();
?>

Please share your knowledge.

Thanks ChatFun


Solution

  • After lots of research finally i got one solution.

    I have created external php file within webroot folder

    i write following code in external_login.php-

    <?php
    if(isset($_REQUEST['login'])){
    session_start();
    //include('forum/SSI.php');
    ?>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Redirecting...</title>
    
    </head>
    
    <body>
    <div style="display:block; position:relative">
    <div style="position:absolute; left:-200px;">
    <form accept-charset="UTF-8" action="<?php echo 'forum/index.php?action=login2';?>" method="post" name="smf_login_frm" id="smf_login_frm">
    <input type="text" name="user" id="user" width="1" style="width:1px;" value="<?php echo $_SESSION['Auth']['User']['email'];?>" />
    <input type="password" id="passwrd" name="passwrd" style="width:1px;" value="<?php echo $_SESSION['passwd'];?>" />
    </form>
    </div>
    </div>
    <script>
    setTimeout(function(){ validateSubscription(); }, 1000);
    
    function validateSubscription() {
            //document.smf_login_frm.submit();
            document.getElementById("smf_login_frm").submit();
            return false;
    }
    </script>
    </body>
    </html>
    <?php
    }else if(isset($_REQUEST['logout'])){
    include('forum/SSI.php');
    
    global $context, $txt, $scripturl;
        $redirect_to = 'http://'.$_SERVER['HTTP_HOST'].'/login';
        $_SESSION['logout_url'] = $redirect_to;
    
        header('location:'.$scripturl . '?action=logout;' . $context['session_var'] . '=' . $context['session_id']);
        //$link = '<a href="' . $scripturl . '?action=logout;' . $context['session_var'] . '=' . $context['session_id'] . '">' . $txt['logout'] . '</a>';
        //echo $link;
    
    
    }else{
    
    }?>
    

    First i have successfully login cakephp. then i will redirect to external_login.php. There i am getting my cakephp login session. So i assign username and password then again i will redirect to smf forum login page. After successfully smf forum login page again redirected to Cakephp home page if i both are login successfully.

    login action in UsersController.php

    public function login() {
            if ($this->Session->read('Auth.User')) {
                $this->redirect($this->webroot);
            } else {
                $this->layout = 'login';
                if ($this->request->is('post')) {
                    $data=$this->request->data;
                    $this->Session->write('passwd',$data['User']['password']);
                    if (!$this->Auth->login()) {
                        $this->Session->setFlash(__('Your Email or Password was incorrect.'), 'error_message');
                    }
                }
                if ($this->Session->read('Auth.User')) {
                    $this->redirect($this->webroot . 'external_login.php?login&hash_token='.md5(time()));
                    exit;
                    /*if ($this->Session->read('Auth.User.type') == 1) {
                        $this->redirect($this->webroot . 'users/index');
                        exit;
                    }
                    if ($this->Session->read('Auth.User.type') == 2 || $this->Session->read('Auth.User.type') == 3) {
                        $this->redirect($this->webroot . 'myprofile');
                        exit;
                    }*/
                }
            }
        }