首页 > 解决方案 > php open and close tag and scope syntax

问题描述

How am I suppose to be using the "<?php" and "?>" syntax properly. I have found out you can mix the open and close tags with open and close braces to allow for other languages in between, but am I doing it correctly? The welcome page is triggering the else statement when it should be entering the if statement.

I want to make each page inaccessible unless the user has logged in, so I it set up this way.

Here is the login file code

    <?php
$uname=$_POST['uname'];
$password=$_POST['password'];
$password = md5($password);

require_once('connection.php');

$query = "SELECT * FROM `login_info` WHERE `uname`='$uname' && `password`='$password'";

$stmt = $pdo->prepare($query);

$stmt->execute();

$count=$stmt->rowCount();
if($count==1)
{
    echo "Login success";
    $_SESSION['log']=1;
    $_SESSION['uname'] = $_POST['uname'];
    header("refresh:1;url=welcome.php");

}
else
{
    echo "Login Credentials Not Found";

    echo"<a href='index.php'><br/><br/>Back to Login Page</a>";
}
?>

and here is the welcome.php

<?php
require_once('connection.php');

session_start();

if(isset($_SESSION['log']))
{
?>

<!DOCTYPE html>
<html>
<style>
#quicklinks 
{
  padding-right: 15px;
}
</style>
<body>
    <h2>LINKS</h2>
    <a id="quicklinks" href="welcome.php" >Welcome</a>
    <a id="quicklinks" href="findwords.php" >search users favourite words</a>
    <a id="quicklinks" href="editwords.php" >edit your favourite word</a>
    <a id="quicklinks" href="antclicker.php" >play the ant game</a>
    <a id="quicklinks" href="antgameleaderboard.php" >ant game world leaderboard</a>
    <a id="quicklinks" href="chat.php" onclick="window.open('chat.php','newwindow','width=450,height=650'); return false;">chat</a>
    <a href="index.php" >logout</a>
    <p>Hello and welcome to my website!</p>
</body>
</html>

<?php
}
else
{
    echo "You are not logged in, please login";

    echo"<a href='index.php'><br/><br/>Login Page</a>";
}

?>

As you can see the php code is broken up around the html code, but the separate php blocks are directly related. I would imagine the simplicity of $_SESSION['log']=1; and if(isset($_SESSION['log'])) should work. The issue is that the welcome page reverts to the else statement, although the 'log' index has to be set, right? This is why I think it must be a scope issue.

标签: phphtmlsqlsessionscope

解决方案


简短回答:您正确使用了语法。除了您忘记session_start();在第一个示例中调用。

更长的答案:您还可以使用替代语法,以使代码更具可读性:

<?php
  require_once('connection.php');

  session_start();

  if(isset($_SESSION['log'])):
?>

<!DOCTYPE html>
<html>
<style>
#quicklinks 
{
  padding-right: 15px;
}
</style>
<body>
    <h2>LINKS</h2>
    <a id="quicklinks" href="welcome.php" >Welcome</a>
    <a id="quicklinks" href="findwords.php" >search users favourite words</a>
    <a id="quicklinks" href="editwords.php" >edit your favourite word</a>
    <a id="quicklinks" href="antclicker.php" >play the ant game</a>
    <a id="quicklinks" href="antgameleaderboard.php" >ant game world leaderboard</a>
    <a id="quicklinks" href="chat.php" onclick="window.open('chat.php','newwindow','width=450,height=650'); return false;">chat</a>
    <a href="index.php" >logout</a>
    <p>Hello and welcome to my website!</p>
</body>
</html>

<?php else: ?>
    You are not logged in, please login
    <a href='index.php'><br/><br/>Login Page</a>
<?php endif; ?>

最后,选择一种风格并坚持下去,以保持一致。


推荐阅读