doupi8598 2012-07-14 16:51
浏览 80

拆分两个php标签之间的IF / ELSE语句,给出Parse错误:语法错误,意外$ end

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

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

This is the code

Include 1:

<?php

session_start();

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

THIS IS THE PROTECTED CONTENT THAT GETS LOADED IF SESSION EXISTS

Include 2:

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

?>

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.

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

Thanks Danny

  • 写回答

3条回答 默认 最新

  • donglian7879 2012-07-14 16:58
    关注

    Rather than using curly braces, you may try using the colon notation as seen below.

    <?php
    
    session_start();
    
    if ($_SESSION['username']):
    ?>
    
    ...
    
    <?php else: header("location: index.php"); endif;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录