简体   繁体   English

HTML中包含的PHP代码不会更新

[英]PHP code included in HTML won't update

I currently have a PHP file which will search my MySQL database and see if a user is logged in. If they are logged in, it will echo "Welcome 'username'. Logout" and if they're not logged in it will echo "Login. Register." 我目前有一个PHP文件,它将搜索我的MySQL数据库并查看用户是否已登录。如果他们已登录,则会回显“欢迎'用户名'。注销”,如果他们没有登录,则会回显“登录。注册。“

If I view this PHP file directly, it will echo out the correct text, depending on whether or not I am logged in. However, if I put into my HTML file using include it will only echo out the logged out text, regardless of whether I'm logged in. 如果我直接查看这个PHP文件,它会回显出正确的文本,具体取决于我是否登录。但是,如果我使用include放入我的HTML文件,它将只回显出已注销的文本,无论是否我已登录

Is there some conflict between PHP and HTML which will stop it from printing out the correct text maybe? PHP和HTML之间是否存在冲突,这会阻止它打印出正确的文本? It seems strange that it will work opening the PHP file itself, but not when it's included in HTML. 看起来奇怪的是它可以打开PHP文件本身,但是当它包含在HTML中时却不行。

HTML code: HTML代码:

<?php include "loginreg/check.php"; ?>

Would the fact it's in a subfolder make a difference? 它在子文件夹中的事实会有所不同吗? Haven't included the PHP code as that itself is working, but I have got it if you need to see it. 没有包含PHP代码,因为它本身正在工作,但如果你需要看到它,我已经得到它。

Cheers 干杯

PHP code: PHP代码:

// Gets IP address
$ip = visitorIP();

// Connect to database
mysql_connect(localhost, $username, $password);
@mysql_select_db($database) or die('Unable to select database');

$query  = "SELECT * FROM loggedin WHERE userip='$ip'";
$result = mysql_num_rows(mysql_query($query));
if ($result == '0') {
    mysql_close();
    loggedOut();
    return;
}

if (isset($_COOKIE['sid'])) {
    $sessionid = $_COOKIE['sid'];
}

$result = mysql_query($query);
while ($row    = mysql_fetch_assoc($result)) {
    if ($row['sessionid'] == $sessionid) {
        mysql_close();
        loggedIn($row['id']);
    } else {
        mysql_close();
        loggedOut();
    }
}

function visitorIP() {
    if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $TheIp = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        $TheIp = $_SERVER['REMOTE_ADDR'];
    }
    return trim($TheIp);
}

function loggedIn($id) {
    global $username, $password, $database;
    mysql_connect(localhost, $username, $password);
    @mysql_select_db($database) or die('Unable to select database');

    $query  = "SELECT * FROM users WHERE id='$id'";
    $result = mysql_query($query);

    while ($row = mysql_fetch_assoc($result)) {
        $fname = $row['fname'];
        $sname = $row['sname'];
    }

    echo "<div class=\"fltrt\">Welcome, " . $fname . ". <a href=\"http://redsquirrelsoftware.co.uk/loginreg/logout.php\">Logout</a></div>";
}

function loggedOut() {
    echo "<div class=\"fltrt\"><a href=\"http://redsquirrelsoftware.co.uk/loginreg/login.html\">Login</a> <a href=\"http://redsquirrelsoftware.co.uk/loginreg/register.html\">Register</a></div>";
}

Without seeing the code of both scripts this is just a guess, but a likely problem would be that you are outputting html (anything...) before you include your loginreg/check.php script. 如果没有看到两个脚本的代码,这只是一个猜测,但可能的问题是,在包含loginreg/check.php脚本之前,您正在输出html(任何...)。

That would render any session_start() statements in your included file useless as the headers already have been sent. 这会使所包含文件中的任何session_start()语句无效,因为已经发送了标头。 And not being able to get to the session would lead to the error that you describe. 并且无法进入会话将导致您描述的错误。

Edit: For cookies the same principle applies, they need to be set before the headers are sent so before you output anything to the browser. 编辑:对于cookie,同样的原则适用,它们需要在发送标题之前设置,因此在向浏览器输出任何内容之前。

Your issue is that you are setting cookies while inside a subdirectory. 您的问题是您在子目录中设置cookie。 Use the path parameter of setcookie to ensure you're setting the cookie in the root folder of your website: 使用setcookiepath参数确保您在网站的根文件夹中设置cookie:

// Sets the cookie for the root of the domain:
setcookie("username", "foo", time() + 60 * 60, "/");

Correct me if I'm wrong here, but are you trying to use a PHP include in an HTML file? 如果我在这里错了,请纠正我,但是你是否想在HTML文件中使用PHP include? If so, that will never work (unless you've got some custom server config that will parse PHP code in HTML files). 如果是这样,那将无法工作(除非您有一些自定义服务器配置将解析HTML文件中的PHP代码)。

PHP code is for PHP files. PHP代码适用于PHP文件。 HTML code can work in HTML and PHP files. HTML代码可以在HTML和PHP文件中使用。 You cannot do a PHP include, in an HTML file. 你不能在HTML文件中做PHP包含。

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

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