duanjiao3686 2014-09-01 09:21
浏览 37
已采纳

会话标志在Javascript中不起作用

I use session flag in javascript for IF function. If session flag is 1, the javascript will show a specifict div on click. I have tried it manually, but the code doesn't seem to work.

This is my JS code:

$(document).ready(function(){
check = <?= $_SESSION['flag'] ?>;
$("#bag").click(function(){
    if(check==0){
        $("#login").show();
        $("#notlogin").hide();
    } else {
        $("#login").hide();
        $("#notlogin").show();
    }
});
}); 

And this is the session in the head of html file:

<?php @session_start();
$_SESSION['flag']=0;
?>

Please check it in the fiddle: http://jsfiddle.net/9mm5ougu/

config-haslogin.php

<?php
error_reporting(E_ALL ^ E_NOTICE); 
ini_set("display_errors", 1);
mysql_connect("mysql.com","name","password") or die("cannot connect");
mysql_select_db("databasename") or die("Fail here");
$myemail= $_POST['myemail'];
$mypassword= $_POST['mypassword'];
$sql= "SELECT * FROM user WHERE myemail='".$myemail."' and mypassword='".$mypassword."'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count==1)
{
echo "Login successful";
$_SESSION['flag']=0;
header("Location:index.php");
}
?>
  • 写回答

3条回答 默认 最新

  • dran0703 2014-09-01 09:53
    关注

    You can't put PHP code into a JavaScript file, because PHP is interpreted on the server side, while JavaScript is interpreted on the client side (at least here in your use case).

    If you really have to do conditional treatment based on the PHP $_SESSION value, you have multiple choices (listed from the worst to the best one IMHO):

    Solution 1: use a dynamic JavaScript file (the worst)

    Put PHP code in your JavaScript file, but use the .php extension instead of .js. Your JavaScript code would look like something like this:

    file.js.php

    $("#bag").click(function(){
        <?php if ($_SESSION['flag'] === 0): ?>
            $("#login").show();
            $("#notlogin").hide();
        <?php else: ?>
            $("#login").hide();
            $("#notlogin").show();
        <?php endif; ?>
    });
    

    And you can include this PHP file as a JavaScript file:

    index.php

    <script src="file.js.php"></script>
    

    This is the worst solution: - as you're mixing both languages, your file will soon become unreadable - because the file is now dynamic, the user's browser can't put it on the client-side cache - you're using PHP server's resources where it's not really necessary - you can't deploy your file on a CDN, or on a simple server dedicated to serve static file - you can't minify your JavaScript file

    Solution 2: use two different JavaScript files

    Create two different JavaScript file, one for logged in user and one for logged out. Load the correct file using the $_SESSION value.

    loggedOut.js

    $("#bag").click(function(){
        $("#login").hide();
        $("#notlogin").show();
    });
    

    loggedIn.js

    $("#bag").click(function(){
        $("#login").show();
        $("#notlogin").hide();
    });
    

    index.php

    <body>
        <!-- page content here -->
    
    
        <?php if ($_SESSION['flag'] === 0): ?>
            <script src="loggedIn.js"></script>
        <?php else: ?>
            <script src="loggedOut.js"></script>
        <?php endif; ?>
    
    </body>
    

    This solution is better than the first one because it resolves almost all points: the file is cached on the client and you don't mix PHP and JavaScript code. But this is not the best solution you can have, because it brings a lot of code duplication and it would be harder to maintain the code base.

    Solution 3: bring the model client side (or sort of)

    You can pass your data model to the JavaScript file, and use it directly from there. As an example, you can have a class name on the <body> tag that depends on the $_SESSION['flag'] value, and your JavaScript file will behave differently based on this value. Here is an example:

    index.php

    <?php
    $className = $_SESSION['flag'] ? 'logged-in' : 'logged-out';
    ?>
    
    <body class="<?php echo $className; ?>">
        <!-- page content here -->
    
        <script src="yourFile.js"></script>
    </body>
    

    yourFile.js

    $(document).ready(function(){
    
        var isLoggedIn = $('body').hasClass('logged-in');
    
        $("#bag").click(function() {
            if (isLoggedIn)
            {
                $("#login").show();
                $("#notlogin").hide();
            }
            else
            {
                $("#login").hide();
                $("#notlogin").show();
            }
        });
    
    });
    

    If this class is only used by the JavaScript code (it means this class will no be used in the CSS code), you should prefix it with this js- to differentiate it from real CSS class names.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀