$ _SESSION函数没有抓取会话变量

时间:2015-07-18 22:26:35

标签: php html5 apache xampp session-variables

使用$id = $_GET["id"]; $sql = "SELECT * FROM personalads WHERE id='$id' LIMIT 1"; $query = mysqli_query($db_conx, $sql); $row = mysqli_fetch_array($query, MYSQLI_NUM); $u = $row[1]; $age = $row[2]; $university = $row[3]; $headline = $row[4]; $message = $row[5]; $img = $row[6]; $picture = ""; if($img != NULL){ $picture = '<img class="adpicture" src="user/'.$u.'/'.$img.'">'; } 函数时,它不会抓取会话变量。我没有在屏幕上弹出任何错误但是当我检查服务器错误日志时我得到了

  

未定义的索引

当我添加print_r($ _ SESSION)时;到我的代码的顶部,但仍然在PHP标签内页面上的所有html消失

$PM = '<div class="adrespond">';
$PM .= '<form onclick="return false;" name="pmForm" id="pmForm" method="post">';
$PM .= '<br><center>Send a message to the poster</center><br>';
$PM .= '<textarea name="pmTextArea" id="pmTextArea" rows="4" cols="30"></textarea>';
$PM .= '<input name="pm_sender_id" id="pm_sender_id" type="hidden" value="'.$_SESSION["userid"].'" />';
$PM .= '<input name="pm_sender_name" id="pm_sender_name" type="hidden" value="'.$_SESSION["username"].'" />';
$PM .= '<input name="pm_rec_name" id="pm_rec_name" type="hidden" value="'.$u.'" />';
$PM .= '<span id="PMStatus" style="color:#F00;"></span><br />';
$PM .= '&nbsp;&nbsp;<center><button name="pmSubmit" id="pmSubmit" onclick="sendPM()" >Send Message</button></center>';
$PM .= '<span id="status"></span>';
$PM .= '</div>';

Array ( [userid] => 1 [username] => jbird [password] => ********
[mysex] => male [lookingfor] => female [university] => Brock ) Array ( [id] => 14 )

当我添加print_r($ _ SESSION)和print_r($ _ GET)(第二个数组来自$ _GET)时,网页上的html消失了,该函数的输出是

[18-Jul-2015 23:40:47 Europe/Berlin] PHP Notice:  Undefined index: id in C:\xampp\htdocs\Website\adcontent.php on line 7

[18-Jul-2015 23:40:47 Europe/Berlin] PHP Notice:  Undefined index: userid in C:\xampp\htdocs\Website\adcontent.php on line 26

[18-Jul-2015 23:40:47 Europe/Berlin] PHP Notice:  Undefined index: username in C:\xampp\htdocs\Website\adcontent.php on line 27

网页上没有打印错误,但是当我查看错误日志时,我看到了这个

<?php
include_once("check_login_status.php");
print_r($_SESSION);
print_r($_GET);
?>
<?php
$id = $_GET["id"];
$sql = "SELECT * FROM personalads WHERE id='$id' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
$row = mysqli_fetch_array($query, MYSQLI_NUM);
$u = $row[1];
$age = $row[2];
$university = $row[3];
$headline = $row[4];
$message = $row[5];
$img = $row[6];
$picture = "";
if($img != NULL){
$picture = '<img class="adpicture" src="user/'.$u.'/'.$img.'">';
}

    $PM = '<div class="adrespond">';
    $PM .= '<form onclick="return false;" name="pmForm" id="pmForm" method="post">';
    $PM .= '<br><center>Send a message to the poster</center><br>';
    $PM .= '<textarea name="pmTextArea" id="pmTextArea" rows="4" cols="30"></textarea>';
    $PM .= '<input name="pm_sender_id" id="pm_sender_id" type="hidden" value="'.$_SESSION["userid"].'" />';
    $PM .= '<input name="pm_sender_name" id="pm_sender_name" type="hidden" value="'.$_SESSION["username"].'" />';
    $PM .= '<input name="pm_rec_name" id="pm_rec_name" type="hidden" value="'.$u.'" />';
    $PM .= '<span id="PMStatus" style="color:#F00;"></span><br />';
    $PM .= '&nbsp;&nbsp;<center><button name="pmSubmit" id="pmSubmit" onclick="sendPM()" >Send Message</button></center>';
    $PM .= '<span id="status"></span>';
    $PM .= '</div>';

// PHP FOR SENDING PRIVATE MESSAGES
if(isset($_POST["sendername"])){
    $pmTA = mysqli_real_escape_string($db_conx, $_POST['pmTextArea']);
    $sname = preg_replace('#[^a-z0-9 ]#i', '', $_POST['sendername']);
    $rname = preg_replace('#[^a-z0-9 ]#i', '', $_POST['recName']);
    $number = "1";
    $sql = "INSERT INTO privatemessages (to_user, from_user, time_sent, message) VALUES ('$rname','$sname',now(),'$pmTA')";
    header("location: personalspage.php");
    $query = mysqli_query($db_conx, $sql) or die("Its not working");
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<link href="style.css" rel="stylesheet" type="text/css">
<script src="jquery-2.1.4.js" type="text/javascript"></script>
<script src="main.js"></script>
<script src="ajax.js"></script>
<script type="text/javascript">
function sendPM() {
      var pmTextArea = _("pmTextArea").value;
      var sendername = _("pm_sender_name").value;
      var recName = _("pm_rec_name").value;
      var status = _("status");
      if (pmTextArea == "") {
           status.innerHTML("Please type in your message.");
      } else {
           var ajax = ajaxObj("POST", "adcontent.php?id=<?php echo $id; ?>"); 
           window.location = "adcontent.php?id=<?php echo $id; ?>";      
      }
       ajax.send("pmTextArea="+pmTextArea+"&sendername="+sendername+"&recName="+recName);
}
</script>
</head>

<body>
<?php include_once("template_pageTop.php"); ?>
<div>
<div class="adcontent">
    <div class="adcontentpicture">
        <?php echo $picture; ?>
    </div>
    <div class="adcontenttext">
        <b>Age:</b><?php echo $age; ?><br><br>
        <?php echo $university; ?> University <br><br>
        <?php echo $headline; ?><br><hr>
        <?php echo $message; ?><br><br><br><br><br><br><br><br>
        <hr><?php //echo $PM; ?>
        <div class="adrespond">
        <form onclick="return false;" name="pmForm" id="pmForm" method="post">
        <br><center>Send a message to the poster</center><br>
        <textarea name="pmTextArea" id="pmTextArea" rows="4" cols="30"></textarea>
        <input name="pm_sender_id" id="pm_sender_id" type="hidden" value="'.$_SESSION['userid'].'" />
        <input name="pm_sender_name" id="pm_sender_name" type="hidden" value="'.$_SESSION['username'].'" />
        <input name="pm_rec_name" id="pm_rec_name" type="hidden" value="'.$u.'" />
        <span id="PMStatus" style="color:#F00;"></span><br />
        &nbsp;&nbsp;<center><button name="pmSubmit" id="pmSubmit" onclick="sendPM()" >Send Message</button></center>
        <span id="status"></span>
        </div>
    </div>
</div>
</body>
</html>

第7行是$ id = $ _GET [&#34; id&#34;];第26和27行是我使用$ _SESSION函数的地方,我使用xampp apache服务器,任何人都知道这里发生的事情真的开始让我失望了。感谢

所有adcontent.php

     public static void main(String args[]) throws IOException {        
        Supermonkey supermonkey = new Supermonkey();
        supermonkey.getComponent("componennt name ");
    }

当设置登录会话变量时

1 个答案:

答案 0 :(得分:0)

我无法确定问题的根源,但在审核您的代码时,我注意到了以下一些事项:

  • 我是否正确地猜测adcontent.php将表单数据提交给自己?如果是这样,您应该明确检查用户是否实际提交了表单,例如通过isset。但是,更好的做法是将AJAX请求的处理外包到一个单独的文件中。
  • 您是否实际通过session_start初始化会话?这是访问会话变量所必需的。

此外,您应始终验证来自用户的数据。在您的示例中,您应该在将整数传递给数据库之前验证或type cast $_GET['id']为整数。