为什么"if(!session_id() Then Do This"会话 ID 已存在时触发条件?



尝试构建一个分 3 个阶段工作的分页:开始、中途岛、结束。

问题:

问题 1. 我需要知道为什么当我按 ENTER 键并加载页面时,以下行永远不会触发。第 4-11 行。

if(isset($_SESSION['form_step']))//Q1. WHY THIS 'IF' NEVER TRIGGERS WHEN PRESSING 'ENTER' AND LOADING PAGE AND THIS LINE IS UNCOMMENTED ?
{
unset( $_SESSION['form_step'] );
session_unset();
session_destroy();
echo "Form Step:"; echo $_SESSION['form_step'];
echo "Line: 10";
}

问题 2.我需要知道为什么当我单击任何编号链接(例如第 2 页、第 3 页、下一页、上一页(时会触发以下 IF,因为会话 ID 已经存在。仅当会话 ID 不存在时,它才应触发。 注意:即使我在Q1中删除/注释掉上面提到的第4-11行,这仍然会被触发。

if(!session_id()) //Q2. WHY THIS IF GETS TRIGGERED WHEN CLICKING ANY NUMBERED PAGE LINKS (ON PAGINATION SECTION (EG PAGE 1 2 3 ETC.)) SINCE SESSION ID ALREADY EXISTS DUE TO ['form_step'] = 'end' ?
{
session_start();
$_SESSION['form_step'] = 'start';
echo session_id();
echo "Line: 20<br>";
}

问题 3. 当分页部分的编号链接(例如。第 2 页、下一页、上一页等(单击,我应该使用 result_count(( 中的 $sql_query_1 重新计算行匹配项,还是应该保持当前代码不变?

注意:我知道这些堆栈流链接,所以不要费心提及它们:

我们应该手动检查mysqli_connect((错误吗?

B(. 调用"mysqli_stmt_prepare"时是否应该手动检查错误?

注意: 我是初学者。仍然在:程序风格准备语句mysqli。 尚未进入:哎呀Pdo。

希望仅以它们格式提供示例代码演示。

我在代码注释上写了几个问题,我感到困惑和困惑。

脚本: 这是一个单页/单页脚本。因此,url 中的不同参数会触发脚本的不同部分(函数(。

  1. 在"开始"阶段,页面加载显示网络表单。 提交表单时,"中途"阶段使用 result_count(( 中的 $sql_query_1 对 mysql 匹配行进行计数。

  2. 然后使用 pagination(( 中的 $sql_query_2 获取行。

  3. 如果您单击分页部分(例如第 2 页、第 3 页、下一页、上一页等(上的任何编号链接,则再次使用 $sql_query_2 从 pagination(( 中获取匹配的行。

初始网址: http://localhost/power.page/pagination_test_simple.php

分页网址示例: http://localhost/power.page/pagination_test_simple.php?form_type=search&query_type=select&form_step=end&page_limit=2&page=2

http://localhost/power.page/pagination_test_simple.php?form_type=search&query_type=select&form_step=end&page_limit=2&page=3

注意:请注意网址中的"form_step="。

开发模式代码:

<?php
error_reporting(E_ALL);
if(isset($_SESSION['form_step']))//Q1. WHY THIS 'IF' NEVER TRIGGERS WHEN PRESSING 'ENTER' AND LOADING PAGE AND THIS LINE IS UNCOMMENTED ?
{
unset( $_SESSION['form_step'] );
session_unset();
session_destroy();
echo "Form Step:"; echo $_SESSION['form_step'];
echo "Line: 11";
}

if(!session_id()) //Q2. WHY THIS IF GETS TRIGGERED WHEN CLICKING ANY NUMBERED PAGE LINKS (ON PAGINATION SECTION (EG PAGE 1 2 3 ETC.)) SINCE SESSION ID ALREADY EXISTS DUE TO ['form_step'] = 'end' ?
{
session_start();
$_SESSION['form_step'] = 'start';
echo session_id();
echo "Line: 20<br>";
}
//$_SESSION['form_step'] = 'start';//DELETE THIS
if(isset($_SESSION['form_step']) && $_SESSION['form_step'] == 'start')//Only run this IF or one of the upcoming ELSEIFs when page is loaded due to page refresh or ENTER pressed or numbered page links clicked on pagination section (eg. page 1 2 3 etc.).
{
echo "Line: 26<br>";//DELETE THIS LINE
if(!isset($_GET['form_type']) && empty($_GET['form_type']))
{
die("Invalid Form!");
}
else
{
$_SESSION['form_type'] = $_GET['form_type'];
echo "Line: 34<br>";//DELETE THIS LINE
if(!isset($_GET['query_type']) && empty($_GET['query_type']))
{
die("Invalid Query!");
}
else
{
$_SESSION['query_type'] = $_GET['query_type'];
echo "Line: 43<br>";//DELETE THIS LINE
if(!function_exists($_SESSION['form_type']))//eg. $_SESSION['search']. This should trigger the search() function.
{
echo "Line: 46<br>";//DELETE THIS LINE
die("Invalid Form!");
}
else
{
echo "Line: 51<br>";//DELETE THIS LINE
$_SESSION['form_type']();
}
}
}
}
elseif(isset($_SESSION['form_step']) && $_SESSION['form_step'] == 'midway')//Only run this ELSEIF or the next one or the previous one when page is loaded due to page refresh or ENTER pressed or numbered page links clicked on pagination section (eg. page 1 2 3 etc.).
{
echo "Line: 59<br>";
die("invalid Query!");
}
elseif(isset($_SESSION['form_step']) && $_SESSION['form_step'] == 'end')//Only run this ELSEIF or the previous one or the IF before that, when page is loaded due to page refresh or ENTER pressed or numbered page links clicked on pagination section (eg. page 1 2 3 etc.).
{
echo "Line: 64<br>";
pagination();
}
?>
<!DOCTYPE HTML">
<html>
<head>
<meta name="viewport" content="width-device=width, initial-scale=1">
</head>
<body>
<?php
function search()
{echo "Line: 79<br>";//DELETE THIS LINE
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>?form_type=<?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=midway&page_limit=2&page=1" method='post' enctype='plain/text'>
<?php
//Added '*' (asterisk) to indicate the 'Text Field' is a 'required' one.
echo "<label for="first_name">First Name *:</label>
<input type="text" name="first_name" placeholder="First Name" value = "">";?>
<br>
<?php
echo "<label for="marital_status">Marital Status *:</label>";
echo "<select name="marital_status">";
echo "<option value="single">Single</option>";
echo "<option value="married">Married</option>";
echo "</select>";
echo "<br>";
?>
<input type="submit" name="search" value="Search">
</form>
<?php
function result_count()
{
echo "result_count function started<br>";//DELETE THIS LINE
if(isset($_SESSION['form_step']) && $_SESSION['form_step'] == 'midway')
{echo "Line: 102<br>";//DELETE THIS LINE
//Connect to Database. (DB_SERVER, BD_USERNAME, DB_PASSWORD, DB_NAME).
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = mysqli_connect("localhost","root","","powerpage");
$conn->set_charset('utf8mb4'); //Always set Charset.
if($conn === false)
{
die("ERROR: Connection Error!. " . mysqli_connect_error());
}
$sql_query_1 = "SELECT COUNT(id) FROM users WHERE first_name = ? AND marital_status = ?";
$stmt_1 = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt_1,$sql_query_1))
{
mysqli_stmt_bind_param($stmt_1,"ss",$_POST["first_name"],$_POST["marital_status"]);             
mysqli_stmt_execute($stmt_1);
mysqli_stmt_bind_result($stmt_1,$row_count);
mysqli_stmt_fetch($stmt_1);
$_SESSION["row_count"] = $row_count;
mysqli_stmt_close($stmt_1);
mysqli_close($conn);
}
$_SESSION['form_step'] = 'end';
echo $_SESSION['form_step'];
echo "Line: 128<br>";//DELETE THIS LINE
pagination();
}
}
function pagination()
{
echo "pagination function started<br>";//DELETE THIS LINE
if(isset($_SESSION['form_step']) && $_SESSION['form_step'] == 'end')
{echo "Line: 137<br>";//DELETE THIS LINE
echo $_SESSION['form_step'];//DELETE THIS LINE
$page_number = $_GET['page'];
$result_per_page = $_GET['page_limit'];
$row_start = (($page_number * $result_per_page) - $result_per_page); //Offset (Row Number that 'Starts' on page).
$row_end = ($page_number * $result_per_page); //Row Number that 'Ends' on page.
$previous_page = $page_number-1;
$next_page = $page_number+1;
echo "Row Start: $row_start<br>";
echo "Row End: $row_end<br>";
//Connect to Database. (DB_SERVER, BD_USERNAME, DB_PASSWORD, DB_NAME).
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = mysqli_connect("localhost","root","","powerpage");
$conn->set_charset('utf8mb4'); //Always set Charset.
if($conn === false)
{
die("ERROR: Connection Error!. " . mysqli_connect_error());
}
$sql_query_2 = "SELECT * FROM users WHERE first_name = ? AND marital_status = ? LIMIT $row_start,$row_end";
//$_SESSION['sql_query_2'] = $sql_query_2;
$stmt_2 = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt_2,$sql_query_2))
{
mysqli_stmt_bind_param($stmt_2,"ss",$_POST["first_name"],$_POST["marital_status"]);
mysqli_stmt_execute($stmt_2);
$result_2 = mysqli_stmt_get_result($stmt_2);
$row_count = $_SESSION["row_count"];
$total_pages = ceil($row_count/$result_per_page);
echo "TOTAL PAGES: $total_pages<br><br>";
echo "Line: 171<br>";//DELETE THIS LINE
while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC))
{
//Retrieve Values.
$id = $row["id"];
$first_name = $row["first_name"];
$middle_name = $row["middle_name"];
$surname = $row["surname"];
$gender = $row["gender"];
$marital_status = $row["marital_status"];
$working_status = $row["working_status"];
echo "Id: $id<br>";
echo "First Name: $first_name<br>";
echo "Middle Name: $middle_name<br>";
echo "Surname: $surname<br>";
echo "Gender: $gender<br>";
echo "Marital Status: $marital_status<br>";
echo "Working Status: $working_status<br>";
}
mysqli_stmt_close($stmt_2);
mysqli_close($conn);
$i = 1;
if($page_number>$total_pages)
{
echo "<a href='http://localhost/power.page/pagination_test_simple.php?form_type=";?><?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=<?php echo $total_pages;?>'><?php echo "<b> ->Final Page<- </b>";?></a><?php 
}
else
{
if($i<$total_pages)
{
echo "<a href='http://localhost/power.page/pagination_test_simple.php?form_type=";?><?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=<?php echo $next_page;?>'><?php echo " Next Page-> ";?></a><?php 
}
while($i<=$total_pages)
{
if($i<$total_pages)
{
echo "<a href='http://localhost/power.page/pagination_test_simple.php?form_type=";?><?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=<?php echo $i;?>'><?php echo " $i ";?></a><?php 
}
elseif($i==$page_number)
{
echo "<a href='http://localhost/power.page/pagination_test_simple.php?form_type=";?><?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=<?php echo $i;?>'><?php echo "<b> $i </b>";?></a><?php 
}
$i++;
}
if($i<$total_pages)
{
echo "<a href='http://localhost/power.page/pagination_test_simple.php?form_type=";?><?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=<?php echo $previous_page;?>'><?php echo " <-Previous Page ";?></a><?php 
}
}
}
//die();
}
}
if($_SERVER['REQUEST_METHOD'] === 'POST')
{
if(isset($_POST['search']))
{
$_SESSION['form_step'] = 'midway';
echo $_SESSION['form_step'];//DELETE THIS LINE
echo "Line: 237<br>";//DELETE THIS LINE
result_count();
}
}
}
?>

我无法添加 commet,但因此您在第 11 行收到错误

if(isset($_SESSION['form_step']))//Q1. WHY THIS 'IF' NEVER TRIGGERS WHEN PRESSING 'ENTER' AND LOADING PAGE AND THIS LINE IS UNCOMMENTED ?
{
unset( $_SESSION['form_step'] );
session_unset();
session_destroy();
echo "Form Step:"; echo $_SESSION['form_step'];
echo "Line: 11";
}

您无法取消设置会话并调用回显,因为已被破坏

if(isset($_SESSION['form_step']))//Q1. WHY THIS 'IF' NEVER TRIGGERS WHEN PRESSING 'ENTER' AND LOADING PAGE AND THIS LINE IS UNCOMMENTED ?
{
//unset( $_SESSION['form_step'] ); This line is problem
//session_unset(); This line is problem
//session_destroy();  This line is problem
echo "Form Step:"; echo $_SESSION['form_step'];
echo "Line: 11";
}

最新更新