php文件中echo出来的html,不能正常渲染,而是直接显示html源码

dx0202406 2016-09-01 11:17:46
开发环境:PHPstorm,WAMP

我的问题:
在PHP文件中使用echo出来的html,不能正常渲染,都是直接显示html源码。
1、操作步骤
(1)PHP登录页面,输入用户名密码后点击登录按钮,(备注:这个页面可以正常渲染)
(2)后台跳转到登录校验的php页面,如果校验成功,php会有两个echo:
echo "<script>alert('登录成功’);</script>";
echo "<script>window.location='index.php';</script>";

正常情况,如果校验成功,页面应该会弹出一个“登录成功”的窗口,点击确认后,应该会跳转到index.php

但实际结果是,页面直接打印html源码,如下
<script>alert('登录成功!');</script><script>window.location='index.php';</script>

请哪位大神能帮帮忙。我应该从那里入手解决该问题。谢谢!
...全文
2009 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
dx0202406 2016-09-02
  • 打赏
  • 举报
回复
刚才继续研究,得出一个结论。也是我这个问题的最终解决方法。 如果使用include.php来统一管理header("Content-Type:text/html;charset=utf-8"); 一定不能将这句代码写在include.php的开头,为了保险起见,可以放在include.php的末尾。

session_start();
//header("Content-Type:text/html;charset=utf-8");
define("ROOT",dirname(__FILE__));
set_include_path(get_include_path().PATH_SEPARATOR.ROOT."/lib".PATH_SEPARATOR.ROOT."/core".PATH_SEPARATOR.ROOT."/configs");

require_once 'configs.php';
require_once 'image.func.php';
require_once 'string.func.php';
require_once 'upload.func.php';
require_once 'page.func.php';
require_once 'comm.func.php';
require_once 'mysql.func.php';
require_once 'admin.func.php';

header("Content-Type:text/html;charset=utf-8");
我刚才实验了, 1、如果放在我注意的那个地方,页面跳转的时候就报错。 2、如果放在文件末尾,其他的所有跳转都正常了。 结贴了。我把分给@Meteorlet(10分)、@qq_30871693(10分)、@傲雪星枫(20分)
dx0202406 2016-09-02
  • 打赏
  • 举报
回复
@傲雪星枫 我按照您的方法,使用firebug,定位到问题了。。。现在问题已解决。 解决办法:在php文件头上加一句代码:header("Content-Type:text/html;charset=utf-8"); 但我有一个疑问,这句代码,我是写在include.php里头。然后在每个php文件中require_once。 不知道为什么这样做不行,必须得每个php文件中加上header。 希望能得到您的指点。如果有其他朋友知道问题原因,也请您指导。再次,一并都谢谢! 谢谢@Meteorlet、@qq_30871693、@傲雪星枫。
傲雪星枫 2016-09-02
  • 打赏
  • 举报
回复
你可以看看生成后的html中,有没有这两句,看看firebug有没有js错误。
傲雪星枫 2016-09-02
  • 打赏
  • 举报
回复
代码没有问题

function alertMessage($msg, $url)
{
        echo "<script>alert('{$msg}');</script>";
        echo "<script>window.location='{$url}';</script>";
}

alertMessage('test','http://www.baidu.com');
dx0202406 2016-09-02
  • 打赏
  • 举报
回复
@Meteorlet 首先非常感谢您早晨五点多给我回复。非常感谢。按照您的指点,以下是我的几个关键文件代码, 备注:我发现一个奇怪的问题,凡是涉及页面跳转的操作,如果页面中包含php代码,全部均显示html源码,不能正常渲染。 1、登录的php文件

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>登陆</title>
    <link type="text/css" rel="stylesheet" href="styles/reset.css">
    <link type="text/css" rel="stylesheet" href="styles/main.css">
    <!--[if IE 6]>
    <script type="text/javascript" src="../js/DD_belatedPNG_0.0.8a-min.js"></script>
    <script type="text/javascript" src="../js/ie6Fixpng.js"></script>
    <![endif]-->
</head>

<body>
<div class="headerBar">
    <div class="logoBar login_logo">
        <div class="comWidth">
            <div class="logo fl">
                <a href="#"><img src="images/logo.jpg" alt="慕课网"></a>
            </div>
            <h3 class="welcome_title">欢迎登陆</h3>
        </div>
    </div>
</div>

<div class="loginBox">
    <div class="login_cont">
        <form action="doLogin.php" method="POST">
            <ul class="login">
                <li class="l_tit">管理员帐号</li>
                <li class="mb_10">
                    <input type="text" name="username" placeholder="请输入管理员帐号" class="login_input user_icon"></li>
                <li class="l_tit">密码</li>
                <li class="mb_10">
                    <input type="password" name="password" class="login_input password_icon"></li>
                <li class="l_tit">验证码</li>
                <li class="mb_10">
                    <input type="text" name="verify" class="login_input password_icon"></li>
                <img src="getVerify.php" alt=""/>
                <li class="autoLogin">
                    <input type="checkbox" id="a1" class="checked" name="autoFlag" value="1"><label
                        for="a1">自动登陆(一周内自动登陆)</label></li>
                <li><input type="submit" value="" class="login_btn"></li>
            </ul>
        </form>
    </div>
</div>

<div class="hr_25"></div>
<div class="footer">
    <p><a href="#">慕课简介</a><i>|</i><a href="#">慕课公告</a><i>|</i> <a href="#">招纳贤士</a><i>|</i><a href="#">联系我们</a><i>|</i>客服热线:400-675-1234
    </p>
    <p>Copyright © 2006 - 2014 慕课版权所有   京ICP备09037834号   京ICP证B1034-8373号   某市公安局XX分局备案编号:123456789123</p>
    <p class="web"><a href="#"><img src="images/webLogo.jpg" alt="logo"></a><a href="#"><img src="images/webLogo.jpg"
                                                                                             alt="logo"></a><a href="#"><img
                src="images/webLogo.jpg" alt="logo"></a><a href="#"><img src="images/webLogo.jpg" alt="logo"></a></p>
</div>
</body>
</html>
2、登录校验文件doLogin.php

<?php
require_once "../include.php";
$username = $_POST['username'];
$password = $_POST['password'];
$verify = $_POST['verify'];
$verify_seesion = $_SESSION['verify'];
if ($verify == $verify_seesion) {
    $sql = "select * from imooc_admin where username='{$username}' and password='{$password}'";
    $row = checkAdmin($sql);
    if($row){
        $_SESSION['adminName'] = $row['username'];
        //header("Location:index.php");
        alertMessage("登录成功!","index.php");
    }else {
        alertMessage("用户名密码错误,请重新输入!","login.php");
    }
} else {
    alertMessage("验证码错误,请重新输入!","login.php");
}
3、公共函数库

function alertMessage($msg, $url)
{
        echo "<script>alert('{$msg}');</script>";
        echo "<script>window.location='{$url}';</script>";
}

function checkAdmin($sql){
    $link = dbConnect();
    $res = fetchOne($link,$sql);
    return $res;
}

function dbConnect()
{
    $link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWD) or die("数据库连接失败!");
    mysqli_set_charset($link, DB_CHARSET);
    mysqli_select_db($link, DB_DBNAME) or dir("数据库打开失败!");
    return $link;
}

function fetchOne($link, $sql, $result_type = MYSQLI_ASSOC)
{
    $result = mysqli_query($link, $sql);
    $row = mysqli_fetch_array($result, $result_type);
    return $row;
}

qq_30871693 2016-09-02
  • 打赏
  • 举报
回复
是不是if else语句写的有问题,请贴出更多代码进行检查。
Meteorlet 2016-09-02
  • 打赏
  • 举报
回复
代码本身没有问题,请贴出所有代码

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧