OK...私は論理的に意味をなさないものを持っています。誰かが私が犯しているばかげた間違いを指摘してくれることを願っています。ユーザーがログインしているかどうかにかかわらず、メニューを変更したいページがあります。ログイン状態を保存するために、セッション変数 $_SESSION['is_logged_in] を使用しています。たとえば、ログアウトした場合、メニューには login と表示されます。ログインしている場合、同じメニューにログアウトが表示されます。これが論理的な部分のコードです。すべてのコード スニペットについて前もってお詫び申し上げますが、できるだけ明確にしたいと思います。助けてください!
まず、いくつかの空の div がある index.php というメイン ページがあります。
<div id="wrapper">
<div id="header">
<h1>My Page</h1>
<div id="navigation">
</div> <!-- end of navigation div -->
</div> <!-- end if header div -->
<p> </p>
<div id="mainContent">
</div><!-- end mainContent div -->
</div> <!-- end wrapper div -->
このページ (index.php) では、ドキュメントのナビゲーション div の jquery ロードを用意しています。
<script type="text/javascript">
$(document).ready(function(e) {
//load the default menu
$('#navigation').load("menu.php");
});
</script>
OK...これで、menu.php には次の html/php が含まれます。
<?php
session_start();
include 'php/functions.php';
//following is for debugging, you will see later where this is useful
if (is_logged_in())
{
echo "logged in value: " . is_logged_in() . "<br />";
print_r($_SESSION);
}
else
echo "not sure what is going on</br >";
echo "<br />";
?>
<ul id="menu" class="DropdownMenu">
<li class="sub"><a href="#">Account</a>
<ul>
<?php
//this is where it should show one link or the other based on the value in $_SESSION
if (is_logged_in()) {
echo "<li><a href=\"#\" class=\"ClickLogout\">Log Out</a></li>\n";
} else {
echo "<li><a href=\"#\" class=\"ClickLogin\">Log In</a></li>\n";
}
?>
</ul>
</li>
</ul>
わかりました...この時点で、ページが読み込まれると、メニューの上部に、まだログインしていないため、予想どおり「ここで何が起こっているのかわかりません」というデバッグメッセージが表示されます。次に、ログイン リンクをクリックし、ajax を介して、index.php の mainContent div を設定します。
これはmenu.phpにあります:
$(document).ready(function(e) {
var options = {
target: '#mainContent', // target element(s) to be updated with server response
success: reloadMenu, // post-submit callback
delegation: true
};
// post-submit callback
function reloadMenu(responseText, statusText, xhr, $form) {
$('#navigation').load("menu.php");
};
//set the jquery form plugin for the login form
$('.ContentForm').ajaxForm(options);
//load the login form in the mainContent div
$('#wrapper').on("click", ".ClickLogin", function(e) {
$('#mainContent').load("login.php");
});
});
上記のコードは、フォームが送信され、成功すると、index.php ドキュメントの初期ロードと同様に、送信後のコールバックがメニューを再ロードする必要があることを示しています。デバッグすると、リロードされることがわかりますが、セッション変数には何も表示されないため、ログインしていても、ログアウトではなくログインリンクが表示されます。
ここで、私が頭がおかしくないことを示すためにすべてをまとめます。login.php の内容は次のとおりです。
<?php
// start the session
session_start();
include 'php/functions.php';
//this sections handles the post of the form
$show_form = true;
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$email = $_POST['email'];
$password = $_POST['password'];
if (authenticate_user($email, $password)) //this will set $_SESSION variables
{
$show_form = false;
echo "Congratulations " . get_user_name() . ", you are logged in <br />";
//following is for debugging, you will see later where this is useful
if (is_logged_in())
{
echo "logged in value: " . is_logged_in() . "<br />";
print_r($_SESSION);
}
else
echo "not sure what is going on</br >";
}
else
{
echo "Invalid email or password. Try again <br />";
}
}
?>
<?php if ($show_form) { ?>
<div id="formContent">
<h2 class="ContentHeader">Log In</h2>
<form id="contentForm" name="registration" class="ContentForm" method="post" action=<?php echo $_SERVER['PHP_SELF']?>>
<p>
<label for="email">Email:</label>
<input type="email" name="email" id="email" required="required">
</p>
<p>
<label for="password">Password:</label>
<input type="password" name="password" id="password" required="required">
</p>
<p>
<input type="submit" name="insert" id="insertLink" value="Submit It" />
<input type="reset" name="cancel" id="cancel" value="Reset" />
</p>
<p>Forgot Password? Click <a href="#">here</a></p>
<p>Need to Register? Click <a class="ClickRegister" href="#">here</a></p>
</form>
私はそれがたくさんのコードであることを知っているので、1つのことを指摘したいと思います. この時点で、フォームが送信されると、#navigation div で menu.php がリロードされ、#mainContent div で login.php がリロードされます。どちらもこのまったく同じコードを実行しています:
//following is for debugging, you will see later where this is useful
if (is_logged_in())
{
echo "logged in value: " . is_logged_in() . "<br />";
print_r($_SESSION);
}
else
echo "not sure what is going on</br >";
ログインフォームの送信後にページが表示されると、メニューの上に「何が起こっているかわからない」と表示され、その後に「Array()」が続きますが、mainContent では「ログイン値: 1」と表示され、すべてのセッション値。
完全を期すために、 is_logged_in() 関数は次のようになります。
//return true if the current session is logged in
function is_logged_in()
{
if (array_key_exists('is_logged_in', $_SESSION))
return $_SESSION['is_logged_in'];
}
menu.php と login.php の両方に session_start() があります。そうしないと、$_SESSION 配列にアクセスしようとすると、他のあらゆる種類のエラーが発生します。
誰かが私の間違いを見つけてくれることを本当に願っています。助けてください!
前もって感謝します。