刷新页面或按钮上的php会话提交

时间:2017-09-12 07:02:15

标签: php

我的网站网址如下:http://localhost/record/index.php 当我添加,更新,删除表中的记录然后我的网址如下:http://localhost/record/index.php?delmsg=Users%20has%20been%20deleted 如何在刷新页面上删除此php变量或使用会话

提交按钮

2 个答案:

答案 0 :(得分:0)

您正试图从GET变量中删除此消息处理,使用会话要好得多,让GET控制逻辑不安全,就像一些用户指出的那样。

在要处理会话值的每个PHP文件的开头使用session_start();

设置您想要稍后阅读的值:

if (isset($_GET['del'])) {
    $del_id = intval($_GET['del']); // intval will improve security
    if (mysqli_query($sql,"Delete from user where id = ".$del_id.";")) {
        $_SESSION["delmsg"] = "User ID = " . $del_id . " has been deleted";
        header('Location: index.php');
    }
}

要在另一个请求中读取该值并相应地显示内容:

if (isset($_SESSION["delmsg"])) {
    echo "<div class='message'>".$_SESSION["delmsg"]."</div>";
    // clean up the session variable value if you don't need it anymore
    unset($_SESSION["result"]);
}

此代码将帮助您了解会话变量,但此代码中仍存在安全漏洞,请勿使用它。首先,了解SQL注入:http://php.net/manual/en/security.database.sql-injection.php和PDO:http://php.net/manual/en/book.pdo.php

希望它有所帮助。

详细了解$_SESSION数组:http://php.net/manual/en/reserved.variables.session.php

答案 1 :(得分:-1)

成功添加,更新或删除记录后,使用下面的header将您的网页重定向回同一页面。

header('location:yourpage.php');
exit;