Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 1 of 2
Use Magento Login functions outside of magento
 
glenelkins
Member
 
Total Posts:  75
Joined:  2008-09-02
 

Hi

I have writte some code that registers a user on the website, but also inserts data into Magento tables so there is also a user account with the magento system ( its all on the same site )

Now, how would I then link my main site login form to magento so the users only login once on the main site and can access magento account areas from there?

For example.. 

MAIN SITE -> LOGIN PAGE -> LOGGED IN ON SITE & MAGENTO

I know there is a class for the user accounts, but that class extends another, which extends another...is there a way around this?

 
Magento Community Magento Community
Magento Community
Magento Community
 
edlitmus
Jr. Member
 
Total Posts:  25
Joined:  2008-07-21
 

I am doing something similar, using magento to manage logged in users. I have two separate machines in my case, one runs magento and one runs Oracle UCM. There is a proxy set up to allow ajax calls to work across the sites, then I created a php script on the magento server to process the login info, using the magento classes.

Here is the javascript for login (using the prototype lib):

function login() {
    
new Ajax.Request(url{
      method
'post',
      
parameters{ logintrueusername$F('username'), password$F('password'},
      
onLoading: function() {
          loading
();
      
},
      
onSuccess: function() {
        finishedLoading
();
        
cart_updated true;
        
getCart();
        
login_updated true;
        
getLoginStatus();
      
}
    }
);
    return 
false;
}

This tells my php script to do this:

require_once('/opt/local/apache2/htdocs/magento/app/Mage.php');
umask(0);

// try to restore session...
if (isset($_COOKIE['magento'])) {
    $session_id 
$_COOKIE['magento'];
    
session_id($session_id);
}
// or set up a new one...
else {
    session_name
('magento'); // XXX - will need to be changed
    
session_start();
}

Mage
::app('default');

$session Mage::getSingleton('customer/session');

if (isset(
$_REQUEST['login'])) {
    login
($_REQUEST$session);
}

function login($args$session{
    
if (!$session->login($args['username']$args['password'])) {
        error_log
("can't log in!");
    
}
}

I also have AJAX calls set to check the login status (true or false) and to logout. The UCM site just includes my javascript and uses the functions to determine if the user is logged in or not and shows the correct html (either the login form, or a welcome message).

 
Magento Community Magento Community
Magento Community
Magento Community
 
glenelkins
Member
 
Total Posts:  75
Joined:  2008-09-02
 

hi

this could work, thanks ill give it a try.

But a question, arent the sessions stored in the database on mageno? your code has no session insertion query.

... AH never mind you use the magento login function

 
Magento Community Magento Community
Magento Community
Magento Community
 
edlitmus
Jr. Member
 
Total Posts:  25
Joined:  2008-07-21
 
glenelkins - 02 September 2008 05:35 AM

hi

this could work, thanks ill give it a try.

But a question, arent the sessions stored in the database on mageno? your code has no session insertion query.

… AH never mind you use the magento login function

Right, magento does all the work.

 
Magento Community Magento Community
Magento Community
Magento Community
 
glenelkins
Member
 
Total Posts:  75
Joined:  2008-09-02
 

sorry what do you mean in this comment?  // XXX - will need to be changed

?

 
Magento Community Magento Community
Magento Community
Magento Community
 
glenelkins
Member
 
Total Posts:  75
Joined:  2008-09-02
 

Hi,

It didnt work so far!! here is my code , this is an AJAX script called from javascript on the page. It logs in on the main site that uses textpattern cms, then youll see where i modified your example code for magento. Logs into my main site ok but then if i navigate to magento its not logged into it!:

<?php
session_start
();

// Get username and pass
$username $_GET['username'];
$password $_GET['password'];
$errors "";

// make database connection
include_once ( "../textpattern/config.php" );
include_once ( 
"table_def.php" );

mysql_connect $txpcfg['host']$txpcfg['user']$txpcfg['pass');
    
mysql_select_db $txpcfg['db');
    
// Check user exists
$result mysql_query "SELECT `id`, `password`,`active` FROM `" SUBSCRIBERS_TABLE "` WHERE `username` = '$username'" );

if ( 
mysql_num_rows $result ) > {

    $result 
mysql_fetch_array $result );
    
    if ( 
$result['active'== '1' {
    
        
if ( $result['password'== md5 $password ) ) {
         
            
// Get payment info
            
$presult mysql_query "SELECT * FROM `" PAYMENTS_TABLE "` WHERE `subscriber_id` = '$result[id]' ORDER BY `date` DESC LIMIT 0,1" );
            
            
$paid 1;
            
            if ( 
mysql_num_rows $presult ) > {
            
                
// Check the last pay date
                
$presult mysql_fetch_array $presult );
                
                
$today strtotime date "Y-m-d"time() ) );
                
$last_paid strtotime $presult['date');
                
                if ( ( 
$today $last_paid ) >= PAY_FREQUENCY {
                    
                    $paid 
0;
                    
                
}
                
            } 
else {
            
                $paid 
0;
                
            
}
            
            $_SESSION[SESSION_PREFIX 
'user_id'$result['id'];
            
$_SESSION[SESSION_PREFIX 'tusername'$username;
            
$_SESSION[SESSION_PREFIX 'paid'$paid;
            
            
// Now login on MAGENTO
            
require_once ( "../magento/app/Mage.php" );
            
            
// try to restore session
            
if ( isset ( $_COOKIE['magento') ) {
            
                $session_id 
$_COOKIE['magento'];
                
session_id $session_id );
                
            
else {
            
                session_name 
'magento' );
                
//session_start();
                
            
}
            
            Mage
::app('default');
            
            
$session Mage::getSingleton('customer/session');
            
            if ( !
$session->login $_GET['email']$_GET['password') ) {
                
                error_log 
"can't log in!" );
                                    
            
}
            
        } 
else {
        
            $errors 
.= "Password Invalid<br>";
            
        
}
        
    } 
else {
    
        $errors 
.= "Your Account Is Not Active!<br>";
        
    
}
    
else {

    $errors 
.= "Username Does Not Exist!<br>";
    
}

if ( empty ( $errors ) ) {

    
echo ( "OK" );
    
else {

    
echo ( $errors );
    
}

?>
 
Magento Community Magento Community
Magento Community
Magento Community
 
edlitmus
Jr. Member
 
Total Posts:  25
Joined:  2008-07-21
 
glenelkins - 02 September 2008 06:09 AM

sorry what do you mean in this comment?  // XXX - will need to be changed

?

Ignore that. It’s just a note to myself that I forgot to take out.

 
Magento Community Magento Community
Magento Community
Magento Community
 
edlitmus
Jr. Member
 
Total Posts:  25
Joined:  2008-07-21
 

It looks like you are trying to manage two sessions at the same time, one for your site then again for magento. That won’t work. To use my technique you have to let magento do all the work, including managing the session.

 
Magento Community Magento Community
Magento Community
Magento Community
 
glenelkins
Member
 
Total Posts:  75
Joined:  2008-09-02
 

i just updated the code...i made some mistakes there. here it is, but now i get magento reporting invalid username and password… when its not...i checked the email in the magento table, and the md5() hashed password and they are correct:

<?php
session_start
();

// Get username and pass
$username $_GET['username'];
$password $_GET['password'];
$errors "";

// make database connection
include_once ( "../textpattern/config.php" );
include_once ( 
"table_def.php" );

mysql_connect $txpcfg['host']$txpcfg['user']$txpcfg['pass');
    
mysql_select_db $txpcfg['db');
    
// Check user exists
$result mysql_query "SELECT `id`, `password`,`active`,`email` FROM `" SUBSCRIBERS_TABLE "` WHERE `username` = '$username'" );

if ( 
mysql_num_rows $result ) > {

    $result 
mysql_fetch_array $result );
    
    if ( 
$result['active'== '1' {
    
        
if ( $result['password'== md5 $password ) ) {
         
            
// Get payment info
            
$presult mysql_query "SELECT * FROM `" PAYMENTS_TABLE "` WHERE `subscriber_id` = '$result[id]' ORDER BY `date` DESC LIMIT 0,1" );
            
            
$paid 1;
            
            if ( 
mysql_num_rows $presult ) > {
            
                
// Check the last pay date
                
$presult mysql_fetch_array $presult );
                
                
$today strtotime date "Y-m-d"time() ) );
                
$last_paid strtotime $presult['date');
                
                if ( ( 
$today $last_paid ) >= PAY_FREQUENCY {
                    
                    $paid 
0;
                    
                
}
                
            } 
else {
            
                $paid 
0;
                
            
}
            
            $_SESSION[SESSION_PREFIX 
'user_id'$result['id'];
            
$_SESSION[SESSION_PREFIX 'tusername'$username;
            
$_SESSION[SESSION_PREFIX 'paid'$paid;
            
            
// Now login on MAGENTO
            
require_once ( "../magento/app/Mage.php" );
            
            
// try to restore session
            
if ( isset ( $_COOKIE['magento') ) {
            
                $session_id 
$_COOKIE['magento'];
                
session_id $session_id );
                
            
else {
            
                session_name 
'magento' );
                
session_start();
                
            
}
            
            Mage
::app('default');
            
            
$session Mage::getSingleton('customer/session');
            
            if ( !
$session->login $result['email']md5 $password ) ) ) {
                
                error_log 
"can't log in!" );
                                    
            
}
            
        } 
else {
        
            $errors 
.= "Password Invalid<br>";
            
        
}
        
    } 
else {
    
        $errors 
.= "Your Account Is Not Active!<br>";
        
    
}
    
else {

    $errors 
.= "Username Does Not Exist!<br>";
    
}

if ( empty ( $errors ) ) {

    
echo ( "OK" );
    
else {

    
echo ( $errors );
    
}

?>

ERRORS:

Fatal errorUncaught exception 'Exception' with message 'Invalid login or password.' in C:\Program Files\EasyPHP 2.0b1\www\textpattern\magento\app\code\core\Mage\Customer\Model\Customer.php:82 Stack trace#0 C:\Program Files\EasyPHP 2.0b1\www\textpattern\magento\app\code\core\Mage\Customer\Model\Session.php(122): Mage_Customer_Model_Customer->authenticate('glen.elkins@gew...', '0d107d09f5bbe40...') #1 C:\Program Files\EasyPHP 2.0b1\www\textpattern\scripts\login_validation.php(76): Mage_Customer_Model_Session->login('glen.elkins@gew...', '0d107d09f5bbe40...') #2 {main} thrown in C:\Program Files\EasyPHP 2.0b1\www\textpattern\magento\app\code\core\Mage\Customer\Model\Customer.php on line 82
 
Magento Community Magento Community
Magento Community
Magento Community
 
edlitmus
Jr. Member
 
Total Posts:  25
Joined:  2008-07-21
 

Two things:

1) Remove the session_start() from the top of the file. That get’s set up later in the magento login area. And move the code that does the $_SESSION stuff to after the magento login section.

2) Try passing the password to magento raw, don’t pass it through md5() first. I think magento handles that on it’s own.

 
Magento Community Magento Community
Magento Community
Magento Community
 
glenelkins
Member
 
Total Posts:  75
Joined:  2008-09-02
 

ok this code produces no errors but still, magento isnt logged in

// Now login on MAGENTO
            require_once ( "../magento/app/Mage.php" );
                  
            
Mage::app('default');
            
            
$session Mage::getSingleton('customer/session');
            
            
$session->login $result['email']$password );
 
Magento Community Magento Community
Magento Community
Magento Community
 
edlitmus
Jr. Member
 
Total Posts:  25
Joined:  2008-07-21
 

Did you change the session handling?

 
Magento Community Magento Community
Magento Community
Magento Community
 
glenelkins
Member
 
Total Posts:  75
Joined:  2008-09-02
 

Hi

I just wrote a test script with the following code. It gets to output “Logged In”. This is run at http://localhost/textpattern but when I then goto http://localhost/textpattern/magento its NOT logged in, even though its just come through as it is. The strange thing is the cookie exists and its storing the session information in the database table…

<?php

            
            
// Now login on MAGENTO
            
require_once ( "magento/app/Mage.php" );
            
            
// try to restore session
            
if ( isset ( $_COOKIE['magento') ) {
            
//die ( "COOKIE IS SET" );
                
$session_id $_COOKIE['magento'];
                
//die ( $session_id );
                
session_id $session_id );
                
            
else {
                
die ( "COOKIE NOT SET" );
                
session_name 'magento' );
                
session_start();
                
            
}
            
            Mage
::app('default');
            
            
$session Mage::getSingleton('customer/session');
            
            if ( !
$session->login "test@test.com""123456" ) ) {
                
                
//error_log ( "can't log in!" );
                  
echo "Cant Log In";                  
            
else {
            
                
echo "Logged In";
                
            
}
            
?>
 
Magento Community Magento Community
Magento Community
Magento Community
 
glenelkins
Member
 
Total Posts:  75
Joined:  2008-09-02
 

any thoughts on my last post?

 
Magento Community Magento Community
Magento Community
Magento Community
 
edlitmus
Jr. Member
 
Total Posts:  25
Joined:  2008-07-21
 
glenelkins - 02 September 2008 11:36 PM

Hi

I just wrote a test script with the following code. It gets to output “Logged In”. This is run at http://localhost/textpattern but when I then goto http://localhost/textpattern/magento its NOT logged in, even though its just come through as it is. The strange thing is the cookie exists and its storing the session information in the database table…

Here’s what I think is happening: the script creates A session for that customer, the store creates another, they aren’t using the same one apparently. Try adding this after the call to the login code:

$session->login('foo@example.com''password');
$session->setCustomerAsLoggedIn($session->getCustomer());

I’m thinking that might do the trick.

 
Magento Community Magento Community
Magento Community
Magento Community
 
nrhammond
Jr. Member
 
Total Posts:  10
Joined:  2008-08-11
 

I’m trying to do the same thing but I am getting…

Parse error: parse error, unexpected T_CLASS in /var/www/vhosts/domain.com/subdomains/dev/httpdocs/shop/app/Mage.php on line 52

and I am just including the file, no code written yet.

This happens when magento is in a subdirectory. nevermind.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 2