1

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

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

这两个登录表单用于日历和 joomla 站点。他们分开工作。它们位于同一个 public_html 目录中。登录表单提交到两个单独的 index.php 文件。如果我可以让用户通过提交一次表单来分别登录两者,我会很高兴。我怎样才能做到这一点。我正在考虑使用链接到表单中的两者的中间 php 文件,但我不知道该怎么做。

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

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

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

编辑:问题是我不希望人们必须登录两次才能访问网站的两个区域。

4

1 回答 1

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; ?>
于 2011-07-28T14:10:27.987 回答