开发者

JQuery and php-Problem with file_get_contents() after $.ajax

开发者 https://www.devze.com 2023-03-20 08:09 出处:网络
I would like to load a error.html-file after clicking a html-button. Therefore, I have a website with a button and the corresponding click-event.

I would like to load a error.html-file after clicking a html-button. Therefore, I have a website with a button and the corresponding click-event.

$.ajax(
        {
            type: "POST",
            url: "../../../server/check.php",
            data: "login=granted&user=user&site=overview",

            success: function(msg)
            {
                alert(msg); 
            }
        });

In this click event I use $.ajax to call a check.php-script that should load my html file via file_get_contents(). Here's the complicated php-Code:

<?php $homepage = file_get_contents('../client/manage/login/error.html'); echo $homepage; ?>

Now the problem is that开发者_如何学运维 error.html isn't loaded, instead the html-code is returned to the $.ajax call. If i type the url of the check.php directly into the browser, it works.

I know there are several approaches how to load a html file, but I need to do it exactly this way. Actually, there are several other sites that I want to load via this php-script.

Can someone give me a hint please?

Thanks much.

EDIT: OK, I try to be a bit more specific. I've successfully implemented a login-system. This system consists of two html-input fields, one for the username and one for the password. Both are checked against a database. If both are correct, the user is allowed to access some protected html-sites.

Now I wanted to avoid that a user can directly put the urls of the protected sites into the browser's address bar; therefore I created a .htaccess file and forced every request of a protected html site with the help of mod_rewrite to be redirected to the check.php. Here's the .htaccess:

RewriteEngine On
RewriteBase /simulation/client/manage 
RewriteRule ^([a-zA-Z]+)\.html$ ../../server/check.php?page=$1 [L]

You see, all protected html files are in /simulation/client/manage, this is the path where are all protected sites are in. If a user wants to redirect to one of them, he is sent to check.php with the corresponing site he or she wants to access. Here's the php code:

if(strcmp($_POST["login"],"granted") == 0)
{
    $_SESSION['user'] = $_POST["user"];
}
else if(strcmp($_POST["logout"],"logout") == 0)
{
    session_unset();
    $_SESSION=array();
}

if(!isset($_SESSION['user']))
{
        $homepage = file_get_contents('../client/manage/login/error.html');
        echo $homepage;
}
else
{
        $homepage = file_get_contents('../client/manage/'.$page.'.html');
        echo $homepage;
}

So far, so good. In this check.php I check if session-variables are set. They are set only if a user has successfully logged in. If not, the error.html is loaded. This all works well, if a user types the url of one of the protected pages into the browser and is not logged in, file_get_contents loads the error.html page, otherwise one of the protected pages is loaded.

The only problem is that if I call the script via $.ajax, nothing is loaded.


That is the intended behavior of an AJAX call. You are requesting the contents of that URL (your PHP script). If I understand your need correctly you could insert the returned html into the body of the page:

$.ajax(
        {
            type: "POST",
            url: "../../../server/check.php",
            data: "login=granted&user=user&site=overview",

            success: function(msg)
            {
                $('body').html(msg);//ADDED LINE
            }
        });


Ok, in my opinion the best solution for this problem is to read the file with read_file() in php and catch the html-code in the browser. Then I user the following three commands in the ajax success callback function:

$.ajax(
    {
        type: "POST",
        url: "../../../server/check.php",
        data: "login=granted&user=user&site=overview",

        success: function(msg)
        {
           document.open();
       document.write(msg);
       document.close();
        }
    });

This solution is nearly the same as Jake's, but here you can replace the entire document. Thanks for your help anyway.

0

精彩评论

暂无评论...
验证码 换一张
取 消