如何制作可以同时登录两个地方的表单?

时间:2011-07-28 13:27:00

标签: php html forms login simultaneous

如何制作可同时登录两个地方的表格?

http://img577.imageshack.us/img577/3127/calendarlogin.jpg http://img94.imageshack.us/img94/1567/joomlalogin.jpg

这两个登录表单用于日历和joomla站点。他们分开工作。它们位于同一个public_html目录中。登录表单提交给两个单独的index.php文件。如果我可以通过向表单提交一次,可以让用户单独登录,我希望如此。我怎样才能做到这一点。我正在考虑使用一个中间的php文件链接到表单中的两个,但我无法弄清楚如何。

两个表单的用户名和密码字段对所有用户使用相同的值。

编辑:哦,哇,我以为可能有一个简单的解决方案。我试过修改登录功能。还没有完全奏效。将日历与joomla集成的想法似乎有点难。这将是处理会话超时等事情的最佳方式。

请不要再回答了,我想在重新提问之前我会花更多的时间尝试一些东西。

编辑:问题是我不想让人们两次登录才能访问该网站的两个区域。

1 个答案:

答案 0 :(得分:2)

我会避免这样做,但为了学术练习,这就是答案。

<?php
$logged_in = false;
$site1_url = 'http://google.com';
$site2_url = 'http://redis.io';

if(array_key_exists('username', $_POST)
        and array_key_exists('password', $_POST)) {

    // Assume the text input fields are named the same in all three forms
    $fields = array(
        'username' => $_POST['username'],
        'password' => $_POST['password'],
    );

    // Access the first site
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $site1_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    $output1 = curl_exec($ch);
    curl_close($ch);

    // Access the second site
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $site2_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    $output2 = curl_exec($ch);
    curl_close($ch);

    if(strpos($output1, 'Logged In') and
            strpos($output2, 'Signed In')) {
        // set logged_in to true only when the appropriate strings are found
        // in the pages we have just posted onto so that we know that the logins
        // were actually successful.
        $logged_in = true;
    }
}
if(false === $logged_in):
    ?>
    <form action="" method="post">
        <label for="username">Username</label>
        <input type="text" name="username" id="username" value="" />

        <label for="password">Password</label>
        <input type="password" name="password" id="password" />

        <input type="submit" />
    </form>
<?php else: ?>
    <p>You are now logged into the website.</p>
    <p>To access the sites try:</p>
    <ul>
        <li><a href="<?php htmlentities($site1_url); ?>"><?php htmlentities($site1_url); ?></a>
        <li><a href="<?php htmlentities($site2_url); ?>"><?php htmlentities($site2_url); ?></a>
    </ul>
<?php endif; ?>