将Hashtag放入参数's值

Put Hashtag in parameter's value

本文关键字:参数 Hashtag      更新时间:2023-09-26

我试图在用户登录时重定向他。

在我的网站上,你可以从不同的地方登录(在主菜单上,在一些页面上,等等),所以我希望用户登录后,他会被重定向到以前的页面。

我得到了一个关于这个过程的长代码,但问题出在登录面板上。

这是造成问题的代码的一部分,例如,如果我有"index.php#page=games",则页面用户必须在登录后重定向到:

我的代码

HTML

<input type="hidden" id="login_redirect" name="login_redirect" value="index.php#page=games" />

JAVASCRIPT/AJAX-

var dataString = 'username=' + username + '&password=' + password + '&login_redirect=' + login_redirect;
    //send informations 
    $("#message_login").load("php_login_code.php?"+dataString);

PHP

<?php
 $login_redirect = $_GET['login_redirect'];
 ?>
<script type="text/javascript">
                        window.location = <?php echo $login_redirect; ?>; //redirect to game's page
                        location.reload();
                    </script>

问题是:我只得到这个链接:index.php

我应该得到这个链接index.php#page=games

编辑

我认为问题来自Ajax脚本,标签不能作为参数值发送。我怎么能摆脱困境?

您需要对插入URL的任何数据运行encodeURICompoent

index.php#page=games

URL中的散列通常意味着锚定到某个标题,例如:

https://en.wikipedia.org/wiki/Java_(programming_language)#Automatic_memory_management

您应该做的只是一起删除#,并使用一个查询字符串。更简单,更直接。

index.php?page=games
if(isset($_GET['page'])) {
   $login_redirect = $_GET['page'];
}

如果您试图从HTMLDOM中获取值,则需要post数据,而不是发送get请求。