重新排列 PHP 页面,使页眉在 HTML 之前 [英] Rearranging PHP page so header is before HTML

查看:26
本文介绍了重新排列 PHP 页面,使页眉在 HTML 之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的标题没有重定向到正确的页面,因为我相信我在它之前有 HTML 元素.我是 PHP 新手,需要帮助重新排列我的页面,所以情况并非如此.如果我用

替换标题行

print_r("");

它工作得很好,但是我知道这不是使用 JS 的好习惯.一旦展示了一个,我就可以这样了,因为我的网站上没有一个工作!下面是我的 changepassword.php 页面:

<?php包括(storescripts/init.php");保护页面();包括(包括/整体/head.php");如果(空($_POST)===假){$required_fields = array('current_password','password','password_again');foreach ($_POST as $key=>$value) {if (empty($value) && in_array($key, $required_fields) == true) {$errors[] = '带星号的字段为必填';打破 1;}}if ($_POST['current_password'] === $member_data['mem_password']) {if(trim($_POST['password']) !== trim($_POST['password_again'])){$errors[] = '您的新密码不匹配';} else if (strlen($_POST['password']) <6) {$errors[] = '您的密码必须至少为 6 个字符';}} 别的 {$errors[] = '您当前的密码不正确';}}?><身体><?php include ("includes/overall/template_header.php");?><div id="mainDivShort"><h1>更改密码</h1><div id="divBreak"></div><?php include ("includes/overall/column_left.php");?><div id="middleContent"><?php if (isset($_GET['success']) &&isset($_GET['success'])) {echo '您已经注册成功';} 别的 {if(empty($_POST) === false && empty($errors) === true) {change_password($session_mem_id, $_POST['password']);header('位置:changepassword.php?success');} else if (empty($errors) === false) {echo output_errors($errors);}?><form action="" method="post"><ul><li>当前密码*:<br><输入类型=密码"名称="current_password"><li>新密码*:<br><input type="password" name="password"><li>重复新密码*:<br><输入类型=密码"name="password_again"><li><input type="submit" value="Change"></表单><?php }?>

<?php include ("includes/overall/column_right.php");?>

<?php include ("includes/overall/template_footer.php");?></html>

解决方案

这种安排应该有效.我们只是将更改密码 if() 移到您的任何文档上方.注意 die() 命令,它告诉 PHP 停止处理.当我们已指示浏览器执行其他操作时,无需执行脚本的其余部分.

正如 Dan 在下面指出的,您需要确保 include()d 文件不包含任何输出.

$value) {if (empty($value) && in_array($key, $required_fields) == true) {$errors[] = '带星号的字段为必填';打破 1;}}if ($_POST['current_password'] === $member_data['mem_password']) {if(trim($_POST['password']) !== trim($_POST['password_again'])){$errors[] = '您的新密码不匹配';} else if (strlen($_POST['password']) <6) {$errors[] = '您的密码必须至少为 6 个字符';}} 别的 {$errors[] = '您当前的密码不正确';}}if(empty($_POST) === false && empty($errors) === true) {change_password($session_mem_id, $_POST['password']);header('位置:changepassword.php?success=1');死();}?><!DOCTYPE html><身体><?php include ("includes/overall/template_header.php");?><div id="mainDivShort"><h1>更改密码</h1><div id="divBreak"></div><?php include ("includes/overall/column_left.php");?><div id="middleContent"><?php if (isset($_GET['success']) &&isset($_GET['success'])) {echo '您已经注册成功';} 别的 {如果(空($错误)===假){echo output_errors($errors);}?><form action="" method="post"><ul><li>当前密码*:<br><输入类型=密码"名称="current_password"><li>新密码*:<br><input type="password" name="password"><li>重复新密码*:<br><输入类型=密码"name="password_again"><li><input type="submit" value="Change"></表单><?php }?>

<?php include ("includes/overall/column_right.php");?>

<?php include ("includes/overall/template_footer.php");?></html>

My header is not redirecting to the correct page because I believe I have HTML elements before it. I am new to PHP and need help rearranging my page so this isn't the case. If I replace the header line with

print_r("<script>location.href='changepassword.php?success'</script>");

it works perfectly, however I know this is not good practise to use JS. Once have been shown the one, I can so the rest as none of them on my website work! Below is my changepassword.php page:

<!DOCTYPE html>
<html>
<?php
include ("storescripts/init.php");
protect_page();
include ("includes/overall/head.php");
if (empty($_POST) ===  false){
$required_fields = array('current_password','password','password_again');
foreach ($_POST as $key=>$value) {
    if (empty($value) && in_array($key, $required_fields) == true) {
        $errors[] = 'Fields marked with an asterisk are required';
        break 1;
    }
}
if ($_POST['current_password'] === $member_data['mem_password']) {
    if(trim($_POST['password']) !== trim($_POST['password_again'])){
        $errors[] = 'Your new passwords do not match';
    } else if (strlen($_POST['password']) <6) {
        $errors[] = 'Your password must be at least 6 characters';
    }
} else {
    $errors[] = 'Your current password is incorrect';
}
}?>
<body>
<?php include ("includes/overall/template_header.php");?>
<div id="mainDivShort">
    <h1>Change Password</h1>
    <div id="divBreak"></div>
    <?php include ("includes/overall/column_left.php");?>
    <div id="middleContent">
        <?php if (isset($_GET['success']) && isset($_GET['success'])) {
            echo 'You have been registered successfully';
        } else {
if(empty($_POST) === false && empty($errors) === true) {
change_password($session_mem_id, $_POST['password']);
header('Location: changepassword.php?success');
} else if (empty($errors) === false) {
echo output_errors($errors);
}?>
        <form action="" method="post">
            <ul>
                <li>Current Password*: <br> <input type="password"
                    name="current_password">
                </li>
                <li>New Password*: <br> <input type="password" name="password">
                </li>
                <li>Repeat New Password*: <br> <input type="password"
                    name="password_again">
                </li>
                <li><input type="submit" value="Change">
                </li>
            </ul>
        </form>
        <?php }?>
    </div>
    <?php include ("includes/overall/column_right.php");?>
</div>
<?php include ("includes/overall/template_footer.php");?>
</body>
</html>

解决方案

This arrangement ought to work. We just move the change password if() above any of your document. Note the die() command which tells PHP to stop processing. No need to execute the rest of the script when we've instructed the browser to carry on someplace else.

As Dan points out below, you'll want to make sure your include()d files do not contain any output.

<?php
include ("storescripts/init.php");
protect_page();
include ("includes/overall/head.php");

if (empty($_POST) ===  false){
    $required_fields = array('current_password','password','password_again');
    foreach ($_POST as $key=>$value) {
        if (empty($value) && in_array($key, $required_fields) == true) {
            $errors[] = 'Fields marked with an asterisk are required';
            break 1;
        }
    }
    if ($_POST['current_password'] === $member_data['mem_password']) {
        if(trim($_POST['password']) !== trim($_POST['password_again'])){
            $errors[] = 'Your new passwords do not match';
        } else if (strlen($_POST['password']) <6) {
            $errors[] = 'Your password must be at least 6 characters';
        }
    } else {
        $errors[] = 'Your current password is incorrect';
    }
}

if(empty($_POST) === false && empty($errors) === true) {
    change_password($session_mem_id, $_POST['password']);
    header('Location: changepassword.php?success=1');
    die();
}
?><!DOCTYPE html>
<html>
<body>
<?php include ("includes/overall/template_header.php");?>
<div id="mainDivShort">
    <h1>Change Password</h1>
    <div id="divBreak"></div>
    <?php include ("includes/overall/column_left.php");?>
    <div id="middleContent">
        <?php if (isset($_GET['success']) && isset($_GET['success'])) {
            echo 'You have been registered successfully';
        } else {
if (empty($errors) === false) {
echo output_errors($errors);
}?>
        <form action="" method="post">
            <ul>
                <li>Current Password*: <br> <input type="password"
                    name="current_password">
                </li>
                <li>New Password*: <br> <input type="password" name="password">
                </li>
                <li>Repeat New Password*: <br> <input type="password"
                    name="password_again">
                </li>
                <li><input type="submit" value="Change">
                </li>
            </ul>
        </form>
        <?php }?>
    </div>
    <?php include ("includes/overall/column_right.php");?>
</div>
<?php include ("includes/overall/template_footer.php");?>
</body>
</html>

这篇关于重新排列 PHP 页面,使页眉在 HTML 之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆