我正在尝试显示div内容,并在浏览器关闭之前记住它。如何使用php调用和设置<div class="show">Show content</div>
?
with open('logtest.txt', 'r') as logFile:
id_ = None
dictOfItems = {}
lines = []
for line in logFile:
if line.startswith("Id: "):
if id_ is not None:
dictOfItems[id_] = lines
lines = []
id_ = int(line[4:])
else:
lines.append(line)
for key, item in dictOfItems.items():
print(key, item)
答案 0 :(得分:1)
试试这个:
<!--HTML PART-->
<form method="post">
<input type="submit" name="setSession" value="Set Session" />
</form>
<!--PHP PART-->
<?php
if(isset($_POST['setSession'])) {
session_start();
$_SESSION['set'] = true;
}
?>
以上是设置会话的页面。 下面是您在设置或未设置会话时要隐藏/显示的内容。
<!--YOUR PAGE-->
<?php
if($_SESSION['set']) {
?>
<div class="show">Show content</div>
<?php
} elseif(!$_SESSION['set']) {
?>
<div class="show">Set the session first</div>
<?php
}
?>
请记住,始终将:session_start();
放在PHP文件的顶部。
PHP ONLY
<?php
session_start();
$_SESSION['fruit'] = 'apple';
if($_SESSION['fruit'] == 'apple') {
//the part you want to be displayed when there is a session
} else {
//the part you want to be displayed when there is NO session
}
?>
注意:会话将始终设置,因为它将在您访问此页面时设置。