【问题标题】:PHP function is automatically called when page loads, instead of being called through onclick event页面加载时会自动调用 PHP 函数,而不是通过 onclick 事件调用
【发布时间】:2012-10-17 18:46:57
【问题描述】:

我正在尝试将网页的用户重定向到两个不同页面之一,具体取决于他们点击的两个按钮中的哪一个。我的方法是将每个 onclick 事件链接到一个 javascript 函数,该函数又会调用一个 php 函数,该函数使用 header() 函数重定向到适当的页面。不幸的是,这两个 php 函数中的后者,即 insertIntoTable(),在每次页面加载和重定向时都会被自动调用,而用户甚至没有机会查看带有按钮的页面。我想知道这是否是 PHP 扩展方式的产物?我不太明白,因为似乎在 onclick 事件侦听器这样做之前不应调用包装 javascript 函数,并且在 javascript 函数 linked 之前不应调用 php 函数onclick 这样做。这是代码...

<?php
    session_start();
?>
<html>
    <head>
        <script>
            function createATable()
            {
                <?php createATable(); ?>
            }

            function insertIntoTable()
            {
                <?php insertIntoTable(); ?>
            }
        </script>
    </head>
    <body>
        <?php
            // Define our redirection functions for button click events
            function createATable()
            {
                header("Location: http://codd.edu/Project2/createtable.php?<?php echo htmlspecialchars(SID); ?>");*/
            }
            function insertIntoTable()
            {
                header("Location: http://codd.edu/Project2/insertintotable.php?<?php echo htmlspecialchars(SID); ?>");*/
            }

            // Set session variables so that we don't need to worry about whether
            // we're coming from sign in or registration - if $Session['user_name'] 
            // isn't set, we know were coming from the sign in
            if (!$_SESSION['user_name'])
            {
                $_SESSION['user_name'] = $_POST['nametextbox'];
                $_SESSION['user_psswd'] = $_POST['psswdtextbox'];
                echo "<br />setting session variables";
            }
            echo "<br />Not setting session variables";

            // If we aren't setting the session variables, then we are coming from the 
            // registration page, and therefore know that this isn't an admin account
            $_SESSION['is_admin'] = "false";

            // Connect to database
            $conn = mysql_connect("localhost", "601", "23");
                or die('Could not connect: ' . mysql_error());

            // Open database
            mysql_select_db("601", $conn) 
                or die('Could not find database: ' . mysql_error());

            // Get USER tuples, if any
            $user = mysql_query("SELECT * FROM USERS WHERE name='$_SESSION[user_name]' AND password='$_SESSION[user_psswd]'");

            // If there are no entries for this SELECT command, we cannot 
            // allow the user to log in 
            $num_user = mysql_num_rows($user);
            if ($num_user < 1) 
            {
                $_SESSION['is_user'] = "false";
                header("Location: http://codd.edu/Project2/login.php?<?php echo htmlspecialchars(SID); ?>");
                exit;
            } 
            else 
            {
                $_SESSION['user_id'] = SID;
                $_SESSION['is_user'] = "true";
                echo "Welcome ", $_SESSION['user_name'], "!";
                echo "<br />User ID: " . $_SESSION['user_id'];
                echo "<br /> User Password: " . $_SESSION['user_psswd'];
                echo "Is valid user? " . $_SESSION['is_user'];
                session_destroy();

                echo "<button name='createtable' onclick='createATable()'>Create a Table</button>";
                echo "<br /><button name='insert' onclick='insertIntoTable()'>Insert into Table</button>";
            }
        ?>
    </body>
</html>

【问题讨论】:

    标签: php javascript session web-applications post


    【解决方案1】:

    我看不出为什么在页面加载时不应调用createATable()。 PHP 在 javascript 之前在不同的环境中进行评估。此外,PHP 允许您在调用函数后定义/声明一个函数,这正是您正在做的事情。

    伪装成 PHP 解释器:它不知道 javascript 是什么。你甚至可以在&lt;?php createATable(); ?&gt; 行前后用yadayadayada ... qweryasdasd 包裹起来,它就会运行。

    请阅读此内容,看看是否有帮助:http://www.developer.com/tech/article.php/923111/Client-side-Versus-Server-side-Coding---Part-1.htm

    PS.:这与您的问题无关,但请看一下:http://en.wikipedia.org/wiki/SQL_injection

    【讨论】:

    • 好的,我明白了。感谢 gd1,完全有道理。
    • 所以我认为最好的办法是完全转储 php 函数并使用 javascript 的 window.location 变量进行重定向,并在会话 ID 后附加 ?
    • 哦,我肯定它很容易受到 sql 注入的影响,但“数据”都是虚拟的,只是试图让逻辑正常运行。
    • 是的,使用 javascript 移动到其他页面。实际上你甚至可以抑制 SID 部分,因为会话 cookie 很少被拒绝。
    • 对于 SQL 注入,如果你修复它是可以的,但请允许我建议你不要这样做(制作一个带有致命错误的存根并稍后改进它)因为有时这些缺陷不会修好,然后就留在那儿。
    【解决方案2】:

    php 在服务器上运行,htnl 输出到客户端。

    不要尝试使用 php 来重定向,而是使用 javascript 或简单的 a href。

    【讨论】:

    • 是的,我现在可以看到了。谢谢大家。
    猜你喜欢
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    • 2012-05-27
    相关资源
    最近更新 更多