php - 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.
解决方案
简短回答:您正确使用了语法。除了您忘记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; ?>
最后,选择一种风格并坚持下去,以保持一致。
推荐阅读
- python - 在 python3 中读取 zip 文件,与 python2 相同
- php - react 和 php 和 mobile
- linux - 如何使用特定核心或核心执行应用程序?
- python - 传递正确的 SQL 语句——正确的方法是什么?
- typescript - Ionic 3 诊断插件不工作抛出 aapt 错误和 processDebubgResource 错误
- javascript - 有条件地为活动/非活动用户呈现徽章
- docker - 在 GKE 工作节点中升级 docker 版本
- tensorflow - 如何使用 Keras 修剪某些权重(而不是冻结层)?
- authentication - 如何在 Flutter 应用中集成 Azure AD SSO
- r - 绘制所有观察结果,但根据不同的组对它们进行着色