dongzaheng4449 2011-11-07 21:12
浏览 43
已采纳

“不推荐使用:session_is_register()”错误 - PHP

First of all, I just started PHP. I know my methods might not be the most efficient, but please try to remember that I'm new to all of this. Examples work great.

I'm trying to write a simple php code for that asks the user three questions. One true/false, one multiple choice and one short answer. Each question is displayed on its own page.

When I try to run this, I get the following error:

 Error: Deprecated: Function session_is_registered() is deprecated on line 10

This script still runs with this error. The problem though is at the end of the quiz, the variable $correct does not seem to change. I printed the variable to see if it was changing and nothing gets printed so obviously something is wrong -- possibly the deprecated function error is the culprit.

Any help would be appreciated.

<?php
ini_set('session.gc_maxlifetime',900);
//echo ini_get("session.gc_maxlifetime"); 
session_start();
if($_SESSION['loggedin'] !== 1) {
header('Location: login.php');
exit;
}

if (!session_is_registered("loggedin"))
{
 $_SESSION["number"] = 0;
 $_SESSION["correct"] = 0;
}

$total_number = 3;

print <<<TOP
<html>
<head>
<title> History Quiz </title>
</head>
<body>
<h3> History Quiz </h3>
TOP;

$number = $_SESSION["number"];
$correct = $_SESSION["correct"];


if ($number == 0){
print <<<FIRST
<p> You will be given $total_number questions in this quiz. <br /><br/>
  You will have 15 minutes to complete it. <br /><br/>
  You cannot go back to change previous answers.<br /><br/>
  Here is your first question: <br /><br />
</p>
<p>1. Abe Lincoln was born in Illinois.</p>
<p>
    <label><input type="radio" name="question1" value="true" /> True </label>
    <label><input type="radio" name="question1" value="false" /> False </label>
</p>
FIRST;

if (isset($_POST['submit'])) {

$selected_radio = $_POST['question1'];

if ($selected_radio == 'false') {
   $correct++;
   $_SESSION["correct"] = $correct;
   print $correct;
}
}
}


if ($number == 1){
print <<<SECOND
<p>2. In what state was the battle of Gettysburg fought?</p>
<p>
    <label><input type="checkbox" name="question2" value="Texas" /> a) Texas </label><br/>
    <label><input type="checkbox" name="question2" value="Pennsylvania" /> b) Pennsylvania   
 </label><br/>
    <label><input type="checkbox" name="question2" value="Virginia" /> c) Virginia </label><br/>
    <label><input type="checkbox" name="question2" value="West Virginia" /> d) West Virginia     
 </label>
</p>
SECOND;

if (isset($_POST['submit'])) {

$selected_checkbox = $_POST['question2'];

if ($selected_checkbox == 'Pennslyvania') {
   $correct++;
   $_SESSION["correct"] = $correct;
   print $correct;
}
}
}


if ($number == 2){
print <<<THIRD
<p>3. The last name of the commander of the Army of North Virginia was __________.</p>
<p>
    <input type='text' id='question3' />
THIRD;

if (isset($_POST['submit'])) {

$selected_answer = $_POST['question3'];

if ($selected_answer == "lee") {
   $correct++;
   $_SESSION["correct"] = $correct;
}
}
}


if ($number >= $total_number)
{
print <<<FINAL_SCORE
Your final score is $correct correct out of $total_number. <br /><br />
Thank you for playing. <br /><br />
FINAL_SCORE;
session_destroy();
}
else
{
$number++;
$_SESSION["number"] = $number;
$script = $_SERVER['PHP_SELF'];
print <<<FORM
<form method = "post" action = $script>
<input type = "submit" value = "Check Answer" />
</form>
FORM;
}

print <<<BOTTOM
</body>
</html>
BOTTOM;
?>
  • 写回答

1条回答 默认 最新

  • duanpo2037 2011-11-07 21:16
    关注

    function session_is_registered() is depcrecated, just use something like isset($_SESSION['blah']) to check

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c