【问题标题】:Programmatically magento admin connection doesn't work以编程方式 magento 管理员连接不起作用
【发布时间】:2018-01-06 19:15:28
【问题描述】:

我想以编程方式将用户登录到 magento 管理员。管理页面位于 iframe 中,它必须自动重定向到管理仪表板而无需身份验证。我使用了一个在古代帖子中找到的代码,它与 magento 核心源相匹配。代码是:

umask(0);
$app = Mage::app('default');

Mage::getSingleton('core/session', array('name' => 'adminhtml'));

// supply username
$user = Mage::getModel('core/factory')->getModel('admin/user')->loadByUsername($loginadmin);

  if (Mage::getSingleton('adminhtml/url')->useSecretKey()) {
         Mage::getSingleton('adminhtml/url')->renewSecretUrls();
  }

  $session = Mage::getSingleton('admin/session');
  $session->setIsFirstVisit(false);
  $session->setUser($user);
  $session->setAcl(Mage::getResourceModel('admin/acl')->loadAcl());
  Mage::dispatchEvent('admin_session_user_login_success',array('user'=>$user));

  if ($session->isLoggedIn()) {
      //Redirection vers le dashboard
       $url = "index.php/admico/dashboard";
       header('Location: '.$url);
  }

当我var_dump()数据时,用户存在并且它拥有所有信息,如名字、id 等,而且都是正确的。代码进入最后一个if 并重定向到'index.php/admico/dashboard',因此$session 已正确登录。但是,无论如何,首页显示连接表单,就好像会话未登录并且不是管理员的仪表板。

有人可以帮我找出问题所在吗?

【问题讨论】:

    标签: php magento


    【解决方案1】:

    我对代码进行了两次更改,它在 firefox、safari 和 chrome 上运行良好。在尝试此代码之前,我还清除了我的 cookie。

    test.php

    <iframe src="http://localhost.site/test_login.php" width="100%"></iframe>
    

    test_login.php

    <?php
    
    require 'app/Mage.php';
    
    umask ( 0 );
    Mage::app ( 'admin' );
    
    Mage::getSingleton('core/session', array('name' => 'adminhtml'));
    
    // supply username
    $user = Mage::getModel('admin/user')->loadByUsername("USERNAME");
    
    if (Mage::getSingleton('adminhtml/url')->useSecretKey()) {
        Mage::getSingleton('adminhtml/url')->renewSecretUrls();
    }
    
    $session = Mage::getSingleton('admin/session');
    $session->setIsFirstVisit(false);
    $session->setUser($user);
    $session->setAcl(Mage::getResourceModel('admin/acl')->loadAcl());
    Mage::dispatchEvent('admin_session_user_login_success',array('user'=>$user));
    
    if ($session->isLoggedIn()) {
        //Redirection vers le dashboard
        $url = "/admin/dashboard/";
        header('Location: '.$url);
    }
    

    【讨论】:

    • 我复制粘贴了你的代码,但它仍然不起作用,它不会将我重定向到仪表板:/
    【解决方案2】:

    也许当窗口在您的最后一个条件中被重定向时,iframe 无法访问您网站上的登录会话。我看到您正在使用 PHP header 函数。我能想到的唯一可能的解决方案是获取登录会话的 SID 并将其用作 URL 参数。所以你的条件中的一些编辑代码看起来像这样:

    $SID=$session->getEncryptedSessionId();
    $url = "index.php/admico/dashboard?SID=" . $SID;
    

    如果这不起作用,您可以尝试使用 PHP 函数 setcookie()$session 作为存储数据,然后尝试重定向。您可以找到该here 的文档。这就是我为你准备的一切。如果这不起作用,请尝试查看 this 并查看是否有任何可以帮助您的东西。祝你好运!

    【讨论】:

      猜你喜欢
      • 2017-05-01
      • 2012-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-30
      • 1970-01-01
      相关资源
      最近更新 更多