ユーザーが自分のユーザー名とパスワードを入力し、フォームの$_POSTメソッドを利用する送信ボタンをクリックするとします。正常にログインするには、明らかにユーザー名とパスワードがmysqlデータベースにあるものと一致している必要があります。これは一連の「if」ステートメントを介して行われます。クレデンシャルが間違っている場合に正しいエラーメッセージを表示するには、formステートメントと"if"ステートメントをhtmlタグ内に配置する必要があります。ユーザー名とパスワードがhtmlタグ内にあるすべての「if」ステートメントを正常に満たした後、明らかにCookieを設定したいと思います。ただし、htmlタグ内にCookieを設定できません。
/*setcook() function can ONLY be placed HERE before the <html> tags, but that does not work with my php code*/
<html>
<head><title></title><body>
<?php
if (isset($_POST['username']) OR isset($_POST['password']))
{
/*bunch of "if" statements go here to confirm the credentials are correct and match what's in the database. if the username and password are correct, all of the "if" statements here are passed, and then i WANT to set a cookie HERE so the user is logged in but i can't*/
}
else echo <<<_END
<form action="login.php" method="post">
Username: <input type="text" name="username"/>
Password: <input type="password" name="password" />
<input type="submit" value="Submit" />"; //this is the form that the user fills out and submits
</form>
_END;
?>
</body>
</html>
ただし、setcookie()関数は、htmlタグの前でのみ機能します。htmlタグ内にあるPHPコードですべてのユーザー名とパスワードのクレデンシャルが確認された後、Cookieを設定するにはどうすればよいですか?