在两个php标记之间拆分IF/ELSE语句,给出解析错误:语法错误,意外的$ end [英] Splitting IF/ELSE statement between two php tags, gives Parse error: syntax error, unexpected $end

查看:111
本文介绍了在两个php标记之间拆分IF/ELSE语句,给出解析错误:语法错误,意外的$ end的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了一个PHP登录系统,该系统将在加载每个页面之前启动会话并检查用户是否已登录.

I have built a PHP login system which starts session before loading each page and checks if the user is logged in.

在受保护"页面上,我在顶部启动了一个包含项,以启动会话,并在if语句中确定是否加载该页面.然后在底部,我还有另一个include,这是else语句,重定向到登录页面.在直接进入页面时,此方法工作正常,但是从其他位置包含时,出现此错误:解析错误:语法错误,意外的$ end

On my Protected page I have a include at the top that starts the session and the if statement as to load the page or not. And then a the bottom I have another include which is the else statement redirecting back to the login page. This works fine when in the page direct, but when included from elsewhere I get this error: Parse error: syntax error, unexpected $end

这是代码

包括1:

<?php

session_start();

if ($_SESSION['username'])
{
?>

如果存在会话,这就是被加载的受保护内容

包括2:

<?php
}
else
header ("location: index.php");

?>

我认为这可能是由于将语句上的花括号分成两个PHP标记引起的,我这样做的原因是,如果if语句为true,则加载整个页面,而不是将其保留在PHP中并回显每行HTML都分开.

I think this might be caused by splitting the curly braces on the statement into two PHP tags, the reason why I have done this is to load a whole page if the if statement is true, rather than leaving it in PHP and echoing each line of HTML seperate.

我该如何解决?还是我有更好的方法拆分IF/ELSE语句.

How can I resolve this? or is there a better way for me to split the IF/ELSE statement.

谢谢 丹尼

推荐答案

当前无法在文件之间打开和关闭大括号.如果尝试执行此操作,将导致意外的$ end"语法错误.

Opening and closing curly brackets in between files is currently not possible. If you attempt to do this it will result in an "unexpected $end" syntax error.

我不确定您要拍摄什么,但是根据您的评论,您应该执行以下操作.

I'm not sure what exactly you are shooting for but based on your comments you should be doing something like the following.

您应该有1个文件,您可以将其命名为任何文件,但我只是将其命名为"allProtectedContent.php"

You should have 1 file you can name it whatever, but i'm just going to call it "allProtectedContent.php"

您说会有多个文件需要保护(我假设您的意思是:如果会话存在,则需要执行多个php文件).因此,在allProtectedContent.php内部,您可以包含所有要执行/输出的文件文件.

You said that there would be multiple files that need to be protected (which by that I assume you mean: multiple php files that need to be executed if the session exists). So inside of allProtectedContent.php you can include all the file files that you want to be executed/output.

然后在主文件中,只需添加以下内容即可:

Then in your main file you should simply have this:

<?php
if(isset($_SESSION['username']))
{
    include 'allProtectedContent.php';
}
else
{
    header('Location: index.php');
}
?>

因此,只有设置了$ _SESSION中的用户名密钥,您输入allProtectedContent.php中的所有内容才会被执行/输出.

So then anything you put in allProtectedContent.php will only be executed/output if the username key in $_SESSION is set.

allProtectedContent.php的示例:

Example of allProtectedContent.php:

<?php
// verify session
// echo some stuff
// include more files
// execute some functions
// run any php code you want
?>

这篇关于在两个php标记之间拆分IF/ELSE语句,给出解析错误:语法错误,意外的$ end的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆