php - 如何在未设置会话开始的类中检查用户是否登录并切换按钮?
问题描述
我在这个网站上阅读了很多答案,谷歌搜索了几乎所有答案,以检查是否存在 isset 会话,但只有在该页面中开始会话时才有效。
我正在尝试检查用户是否已登录并在未设置会话开始的类中切换按钮。
但我收到错误:注意:未定义的变量:第 66 行 D:\wamp\www\demo\classes\CommentSection.php 中的 Suser_ID
第 66 行是:.$this->getLogin($Suser_ID).
这些是我的类:这是从数据库中获取数据的类
Class TreeNode {
private $cid = null; // Comment ID
private $sid = null; // Section ID
private $message = ''; // Message text
private $author = ''; // Author name
private $time = ''; // The time when comment has been posted
private $com_uid = ''; // User id when comment has been posted
private $parent = null; // Comment ID of the parent comment
private $children = false; // If the node has children 1 otherwise 0 (hasChildren() method returns true/false)
private $childrenList = null; // Array with children objects
private $sth = null; // The prepared statement that gets the child nodes
public function __construct($sid, $rootNode = false, $sth = null) {
$this->sid = $sid;
if ($this->hasChildren()) {
if ( $sth === null ) {
$handler = new Database();
$this->sth = $handler->prepare('
SELECT `cid`, `com_uid`, `message`, `parent`, `children`, `time`, `author_name` AS `author`
FROM `comment`
WHERE `parent` = :parent
ORDER BY `time` DESC
');
}
else {
$this->sth = $sth;
}
// Bind the cid of the current node as parent and execute the statement
$this->sth->execute(array(
':parent' => $this->cid
));
// If table is empty
if ($this->sth->rowCount() === 0) {
throw new Exception('Error: Database returned no results');
}
// Add children to node
while ($child = $this->sth->fetchObject('TreeNode', array(false, $this->sth))) {
$this->addChild($child);
}
}
else if ($rootNode === true) {
// If this is a root node
// Connect to database
$handler = new Database();
// Get comments from database
$this->sth = $handler->prepare('
SELECT `cid`, `com_uid`, `message`, `parent`, `children`, `time`, `author_name` AS `author`
FROM `comment`
WHERE `sid` = :sid
AND `parent` IS NULL
ORDER BY `time` DESC
');
// Bind the ID of the comment section and execute the statement
$this->sth->execute( array(
':sid' => $this->sid
));
// If table not empty
if ($this->sth->rowCount() > 0) {
// Add children to node
while ( $child = $this->sth->fetchObject('TreeNode', array(false)) ) {
$this->addChild($child);
}
$this->children = true;
}
}
}
public function getCid() {
return $this->cid;
}
public function getMessage() {
return $this->message;
}
public function getAuthor() {
return $this->author;
}
public function getTime() {
return $this->time;
}
public function getParent() {
return $this->parent;
}
public function hasChildren() {
if ( $this->children > 0 ) {
return true;
}
else {
return false;
}
}
public function getChildren() {
return $this->childrenList;
}
private function addChild($child) {
$this->childrenList[$child->getCid()] = $child;
}
}
这是显示 html 的类:
class CommentSection {
private $sid = null; // Comment Section ID
private $tree = null;
private $status = true;
private $display = '';
public function __construct($sid) {
if (($this->sid = (int) $sid) <= 0) {
$this->sid = null;
throw new InvalidArgumentException('Section ID must be a positive integer');
}
try {
$this->tree = new TreeNode($this->sid, true);
}
catch (Exception $e) {
$this->status = false;
}
$this->createDisplay();
}
public function doComments() {
echo $this->display;
}
private function createDisplay() {
$this->display .= '<div class="comment-section">';
if ($this->status === false) {
$this->display .= 'An error has been occurred';
}
else if ($this->tree->hasChildren() === false) {
// If no comment exist yet
$this->display .=
'<ul class="message-body" id="message-">
<li class="reply-button">Click to add a comment...</li>
<li style="display: none;" class="msg-text">
<input type="hidden" name="author-name" value="name" class="form-control txtfield">
<input type="text" name="author-surname" placeholder="Surname" class="form-control txtfield">
<input type="hidden" name="sid" value="' . $this->sid . '">
<textarea placeholder="Message" class="form-control"></textarea>
</li>
<li style="display: none;" class="hide-reply-box">Click to hide</li>
</ul>
<ul>
<li>
<ul class="message-body">
</ul>
</li>
<ul>';
}
else {
$this->display .=
'<ul class="message-body" id="message-">
<li class="reply-button">Click to add a comment...</li>
<li style="display: none;" class="msg-text">
<input type="hidden" name="author-name" value="name" class="form-control txtfield">
<input type="text" name="author-surname" placeholder="Surname" class="form-control txtfield">
<input type="hidden" name="sid" value="' . $this->sid . '">
<textarea placeholder="Message" class="form-control"></textarea>
</li>
<li style="display: none;" class="hide-reply-box">Click to hide</li>
</ul>';
// Generate comment markup and return
$this->display .= $this->traverseTree($this->tree->getChildren()); // We don't want to display the pseudo-node so we pass its children
$this->display .= '</div>';
}
$this->display .=
'<div class="error message">
<h3>Error:</h3>
<p></p>
</div>
<div class="warning message">
<h3>Warning:</h3>
<p></p>
</div>';
}
private function traverseTree($tree) {
$display = '<ul>';
foreach($tree as $twig) {
$display .= '<li>
<ul class="message-body" id="message-'. $twig->getCid() . '">
<li class="author">' . htmlentities($twig->getAuthor(), ENT_QUOTES, 'UTF-8') . ':</li>
<li class="comment-msg">' . htmlentities($twig->getMessage(), ENT_QUOTES, 'UTF-8') . '</li>
<li class="reply-button">Click to reply...</li>
<li class="msg-text">
<input type="hidden" name="author-name" value="name" class="form-control txtfield">
<input type="text" name="author-surname" placeholder="Surname" class="form-control txtfield">
<input type="hidden" name="sid" value="' . $this->sid . '">
<textarea placeholder="Message" class="form-control"></textarea>
</li>
<li class="hide-reply-box">Click to hide</li>
</ul>';
// If the node has children inject them in a <ul> tag under parent node
if ($twig->hasChildren()) {
$display .= $this->traverseTree($twig->getChildren());
}
$display .= '</li>';
}
return $display . '</ul>';
}
}
类的Usaqe:
require_once('classes/TreeNode.php');
require_once('classes/CommentSection.php');
$comment_section = new CommentSection($page_id);
我试过什么?
我在问题类 CommentSection 的第二类中创建了一个函数:
private $Suser_ID = '';
public function getLogin($Suser_ID){
if(isset($Suser_ID)){
return $addButton = "comment button";
}else{
return $addButton = "Login link";
}
}
并在类中插入 html :
<ul class="message-body" id="message-">
.$this->getLogin($Suser_ID).
调用函数如下:
if($LoggedIn) {
$Suser_ID = $_SESSION['uid'];
}else{
$Suser_ID = "";
}
require_once('classes/TreeNode.php');
require_once('classes/CommentSection.php');
$comment_section = new CommentSection($page_id);
$comment_section->doComments();
$comment_section->getLogin($Suser_ID);
我尝试了更多示例,但无法在此处全部添加。
感谢所有或任何帮助/帮助。
解决方案
推荐阅读
- python - Python解析添加2个数字的自定义逗号
- python - 如何调用函数来启动 Qthreading?
- c++ - 如何在 Visual Studio C++ 项目中引用 ssleay32.dll
- javafx - 使用 TextFormatter 时无法使用 DatePicker 选择框
- c++ - 在 C++ 中扫描字符串
- javascript - 重用管道中可观察源时的事件顺序是否已定义?
- ruby-on-rails - 当我尝试 gem install bootsnap -v '1.7.2 时出现错误
- java - 矩阵乘法 mapreduce
- git - 无法安装 git - 访问被拒绝错误
- delphi - 如何在执行期间调用在新组件中创建的过程