私は単純なif/else文をJavascript & PHP(jQuery cookie plugin)で試しています。Javascriptとif/else文の両方を実行するPHP
私の問題:このスクリプトが起動するたびに、IFとELSEの両方のステートメントが実行されます。私のクッキーが設定されていても、新しいクッキーが作成され、それが私のデータベースに追加されます。両方の言語を正しく使用していますか?
<script type="text/javascript">
//check to see if cookie is present
if ($.cookie('uid')) {
var userCookie = $.cookie('uid');
alert(userCookie);
}
else {
//create cookie and check to see if it is taken
<?php
$success = false;
while (!$success) {
$cookie = createUniqueCookie();
$success = checkIfCookieIsTaken($cookie);
}
//store into database and set into browser
if ($result = $mysqli->query("INSERT INTO users (cookie) VALUES ('" . $cookie . "')")) {
?>
$.cookie('uid', '<?php echo $cookie ?>', { expires: 730, path: '/' });
var userCookie = $.cookie('uid');
alert('new cookie for user is '+ userCookie);
<?php
}
?>
}