页面加载后剥离查询字符串

时间:2013-06-30 10:34:56

标签: php javascript .htaccess mod-rewrite

即时开发php和我的网页网址就像这样

http://localhost/myweb/?action=news
http://localhost/myweb/?action=my_profile
http://localhost/myweb/?action=my_upload
http://localhost/myweb/?action=my_collection
http://localhost/myweb/?action=chat
http://localhost/myweb/?action=my_friends
etc

例如,在特定页面中有删除功能,可以访问href链接

http://localhost/myweb/?action=my_friends&delete=2414535435 <-friends id

我通过在不查看源

的情况下看到链接,使其变得整洁
a href='#'

并使用javascript访问真实链接

$('.deleteclass').on('click', function () {
        var name= $(this).attr('nameattr');
        if(confirm('You sure want to delete '+name+'?')){
            window.location='?action=my_friends&delete='+this.id;
        }
    });

问题是,在我处理删除并加载页面后,我不想要地址栏上的完整链接。

http://localhost/myweb/?action=my_friends&delete=2414535435

我需要在地址栏上将其恢复原状

http://localhost/myweb/?action=my_friends

有可能吗?通过mod重写也许?

1 个答案:

答案 0 :(得分:2)

浏览器加载页面后,您无法删除网址。

删除或执行其他操作后,只需进行重定向。

header('Location: /myweb/?action=my_friends');

将错误/成功消息添加到会话变量(在执行标题之前在操作脚本中),以便您可以在其他页面上显示它们

$_SESSION['errors'] = "Delete Failed: For Some Reason";

在其他页面上检查会话变量是否存在,显示它然后将其删除(否则它将保留在那里并且页面将继续认为存在错误)

<?php

if(isset($_SESSION['errors'])) {
   //Do some code to show the error
   ...
   unset($_SESSION['errors']); //delete the error messages
}