简体   繁体   English

用户未登录时如何限制PHP中的文件浏览?

[英]How to restrict file browse in php when user is not logged in?

can you explain why -1? 你能解释为什么-1吗? Please dont give me any alternative suggestion.. 请不要给我任何其他建议。

Im trying to implement a file upload, if the user is not logged in, then restrict the user to browse file instead show login popup. 我试图实现文件上传,如果用户未登录,则限制用户浏览文件,而是显示登录弹出窗口。

I have tried like this: 我已经这样尝试过:

fileupload.php fileupload.php

<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">

function checksession()
{
    var flag_restrict;
    $.ajax({
        type : 'POST',
        url : 'check_session.php',
        async: false,
        success : function(response){
            if(response === "failure")
                flag_restrict = "block";
            else
                alert("login popup code");
        }
    });
    alert(flag_restrict); //displaying nothing;

}

<input type="file" id="browse" name="fileupload" onclick="return checksession();">

check_session.php check_session.php

session_start();

if($_SESSION['id'] == session_id())
{
    echo "success";
}
else
{
    echo "failure";
}

Problem is when response===failure , file browse is opening, what am I doing wrong, what is correct way to do this? 问题是response===failure ,文件浏览器打开,我在做什么错,正确的方法是什么?

Your function checksession does not have a return value, it will never prevent the onclick handler to fire. 您的函数checksession没有返回值,它将永远不会阻止onclick处理程序触发。 The success handler of your $.ajax() will be executed asynchronously, as the http response from the server was received. $.ajax()的成功处理程序将异步执行,因为已收到来自服务器的http响应。

Things you should do (conceptually speaking): 您应该做的事情(从概念上来说):

  • Only show the upload button when the user is authenticated. 仅在用户通过身份验证时显示上载按钮。
  • The onclick handle triggers an upload using ajax (no matter if the user is authenticated) onclick句柄使用ajax触发上传(无论用户是否通过身份验证)
  • The serverside upload action will return a "unauthenticated" if the user is not authenticated. 如果用户未通过身份验证,则服务器端上载操作将返回“未经身份验证”。
  • If the upload results in a "unauthenticated" message, show the login form. 如果上载导致“未经身份验证”消息,请显示登录表单。

try this 尝试这个

<input type="file" id="browse" name="fileupload" onclick="return checksession(event);">


    function checksession(e)
{

    $.ajax({
    type : 'POST',
    url : 'check_session.php',
    success : function(response){
                if(response === "failure")
                    e.preventDefault();
                else
                    alert("login popup code");
                }});
}

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

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