繁体   English   中英

我应该使用$ _GET动态加载html吗?

[英]Should I use $_GET to dynamically load html?

我正在使用PHP,MySQL(PDO),JQuery和HTML(CSS)的社交网站上工作。 我现在正在个人资料页面上工作,我想确定用户是在自己的页面上还是在其他页面上,以确定他们将拥有哪些输入。 我想为个人资料页面保留一个框架并加载相应的选项,而不是拥有2个不同的页面(我的个人资料与其他个人资料)。

我正在学习JQuery和PHP,所以现在我在想,在JQuery中,我可以获取$ _GET(?username =)并将其与登录用户的用户ID进行比较,然后加载所需的组件基于此。

或者,我确定使用PHP的用户,然后运行特定的脚本来加载组件。

这些方法会起作用吗,还是有其他/更好的方法来做到这一点?

我认为您无法在JavaScript中使用$_GET

而是有一个URLSearchParams

const url = new URL('http://domain/path?username=someone');
const username = new URLSearchParams(url.search).get('username)'; // someone

在jquery中,您可以将$ .get()与html数据类型一起使用

您可以在JavaScript中回显PHP的GET值。 但是请确保,没有makeItSafe (您必须自行定义),可以利用此代码来执行跨站点脚本攻击:

<script>
var myGlobalUserID = "<?php echo makeItSafe($_GET['userID']); ?>";
</script>

最佳安全实践是不依赖浏览器中发生的任何事情。

这意味着您应该始终找出服务器上的特权(在PHP方面)。

变体1(简单)

假设您已经对用户进行了身份验证,并且在PHP中将用户指定为$currentUser ,其标识为$currentUser->id

现在,该用户希望通过请求网址“ profile?id = 555”来查看某些个人资料

所以(基本上)这是您在PHP方面的工作:

$requestedProfile = (int)$_GET['profile'];
if ($requestedProfile == $currentUser->id) {
    // they want own profile
    show_controls_for_own_profile();
} else {
    // they want someone else's profile
    show_controls_for_other_profile();
}

在这种情况下,jQuery与此处无关。

变体2(单独的请求)

此外,假设您要缓存整个个人资料页面,以将其快速加载到浏览器中,然后再进行ajax加载与用户权限相关联的其他控件。

那么您有两个控制器方法(php脚本,无论如何),每个方法都发挥其自己的作用:

public function showProfile() {
    $requestedProfile = (int)$_GET['profile'];
    if (profile_exists($requestedProfile)) {
        show_the_profile($requestedProfile); // no check here
    }
}

上面的方法将通过其ID(带有空的“ .controls”元素)返回通用概要文件页面,在该页面上,一些jquery代码将要求服务器返回用户相关部分的适当变体。

$.ajax('/user-controls.php')
    .done(function(response) { $('.controls').html(response); });

请注意,它不会告诉服务器任何用户ID-服务器应该已经从会话中知道当前经过身份验证的用户ID!

第二个函数与开头相同,将仅返回控件的HTML:

// Example of getting current profile from server's headers
function get_viewed_profile_id()
{
    // url of previous profile page, e.g. http://example.com/profile?id=555
    $referer = $_SERVER['HTTP_REFERER']; 
    $query = parse_url($referer, PHP_URL_QUERY); // id=555
    parse_str($query, $params); // ['id' => '555']
    return $params['id']; // 555
}

// we should store the authenticated user in session, 
// not rely on anything user sends from their browser
$currentUserId = $_SESSION['user']->id; 
$requestedProfileId = get_viewed_profile_id();

// now that we know both viewed profile and current user,
// we can compare them
if ($requestedProfileId == $currentUserId) {
    // they want own profile
    show_controls_for_own_profile();
} else {
    // they want someone else's profile
    show_controls_for_other_profile();
}

变体3(将PHP变量传递给javascript)

与上面相同,但是您不依赖$_SERVER['HTTP_REFERER'] ,而是将一些逻辑移至Javascript:

浏览器询问:

// look up currently browsed profile from current window url
var q = new URLSearchParams(window.location.search);
var controlsURL = '/user-controls.php?viewedProfile=' + q.get('id');
// ask for controls, specifying the currently browsed profile
$.ajax(controlsURL)
    .done(function(response) { $('.controls').html(response); });

服务器响应:

function get_viewed_profile_id()
{
    return (int)$_GET['viewedProfile'];
}

// we should store the authenticated user in session, 
// not rely on anyhting user sends from their browser
$currentUserId = $_SESSION['user']->id; 
$requestedProfileId = get_viewed_profile_id();

// now that we know both viewed profile and current user,
// we can compare them
if ($requestedProfileId == $currentUserId) {
    // they want own profile
    show_controls_for_own_profile();
} else {
    // they want someone else's profile
    show_controls_for_other_profile();
}

只需在页面上声明一个隐藏字段

<input type="hidden" id="username" value="<?php echo $_GET['username'];?>">

获取其中的用户名值。 然后只需在jquery中获取该值

<script>
$(document).ready(function(){
var username = $("#username").val();
if(username == "abc"){
$("#div_one").show();
$("#div_two").hide();
}
});
</script>

使用if()条件和hide()show()方法加载要显示给用户的组件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM