【问题标题】:Session variables only sets once the page has been refreshed会话变量仅在页面刷新后设置
【发布时间】:2015-03-21 14:03:23
【问题描述】:

只有在第二次加载页面后,我的会话变量才会更改为正确的值。由于在第一次加载页面时正确设置了另一个会话变量(matchid),因此表现出的这种行为很奇怪。 if(review number)设置不正确的变量,设置在代码sn-p的底部。

守则

我为长代码 sn-p 道歉,但我不知道错误在哪里:

if ($stmt = $dbc->prepare("SELECT matchid, user1, user2, user1_accept, 
user2_accept FROM matches WHERE user1_accept = ? or user2_accept = ?   
LIMIT 
1")) {
$stmt->bind_param('ii', $id, $id);  // Bind id to parameter.
$stmt->execute();    // Execute the prepared query.
$stmt->store_result();

 // get variables from result.
$stmt->bind_result($matchid, $user1, $user2, $user1_accept,
 $user2_accept);
$stmt->fetch();
$num_rows = mysqli_stmt_num_rows($stmt);
if ($num_rows == 0){header('Location: /nomatches.php');}    
$_SESSION['matchid'] = $matchid;
}
print_r($_SESSION);

if ($user1 != $id){
echo 'user 1 !=';     
$reviewnumber = 'user2_accept';   
echo $reviewnumber;     
if ($stmt = $dbc->prepare("SELECT aboutme, friend, picture FROM  
userprofile
WHERE id = ? LIMIT 1")) {
$stmt->bind_param('i', $user1);  // Bind id to parameter.
$stmt->execute();    // Execute the prepared query.
$stmt->store_result();

 // get variables from result.
$stmt->bind_result($aboutme, $friend, $picture);
$stmt->fetch(); 
$picture = implode('/', array_map('rawurlencode', explode('/',     
$picture)));
}


if ($stmt = $dbc->prepare("SELECT full_name FROM users WHERE id = ? 
LIMIT 1")) {
$stmt->bind_param('i', $user1);  // Bind id to parameter.
$stmt->execute();    // Execute the prepared query.
$stmt->store_result();

 // get variables from result.
$stmt->bind_result($full_name);
$stmt->fetch(); 
}

}

if ($user2 != $id){
echo 'user 2 !=';
$reviewnumber = 'user1_accept';   
echo $reviewnumber;     
if ($stmt = $dbc->prepare("SELECT aboutme, friend, picture FROM
userprofile WHERE id = ? LIMIT 1")) {
$stmt->bind_param('i', $user2);  // Bind id to parameter.
$stmt->execute();    // Execute the prepared query.
$stmt->store_result();

 // get variables from result.
$stmt->bind_result($aboutme, $friend, $picture);
$stmt->fetch(); 
$picture = implode('/', array_map('rawurlencode', explode('/',
 $picture)));
}


if ($stmt = $dbc->prepare("SELECT full_name FROM users WHERE id = ? 
LIMIT  1")) {
$stmt->bind_param('i', $user2);  // Bind id to parameter.
$stmt->execute();    // Execute the prepared query.
$stmt->store_result();

 // get variables from result.
$stmt->bind_result($full_name);
$stmt->fetch(); 
}


 }

$_SESSION["reviewnumber"] = $reviewnumber;   

变量:reviewnumber,在第一页加载时正确回显,这就是为什么我不明白为什么没有正确设置会话变量。

【问题讨论】:

  • session_start(); 在那里,它在哪里?另外,它是否在使用会话的所有页面中?
  • 会话开始位于页面顶部,也位于所有页面上。奇怪的是,其中一个会话变量第一次设置,而另一个没有。
  • 尝试进一步定义它,甚至在您开始会话的位置下方。
  • 如果使用常量进一步定义,它会在第一个页面加载时设置。然而,这是不可能实现的,因为会话变量的值是由 SQL 查询的结果决定的。我在应该设置会话变量的正上方有一行代码,它与一行代码相呼应,所以我知道程序正在到达那个点。

标签: php sql session


【解决方案1】:

在更改会话值之前打印会话值 - 因此新的会话值仅在下一页加载时显示。如果其他人有类似的问题,这绝对是一个重要的检查。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-10
    • 1970-01-01
    相关资源
    最近更新 更多