私はこれを避けたいと思いますが、学術的な演習のためにこれが答えです。
<?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; ?>