【问题标题】:Not able to transfer unique session values from one page to the another无法将唯一会话值从一个页面传输到另一个页面
【发布时间】:2017-05-09 07:10:19
【问题描述】:

我创建了一个应用程序,我想将唯一值/ID 从一个页面传输到另一个页面。我在这个过程中使用了会话。

以下代码:

<?php session_start();?>
   <?php
#php code to get values
include_once "process/config.php";
$sql = "select * from posts";
static $i=0;
$result = mysqli_query($conn,$sql);
if($result->num_rows > 0){
while($rows = $result->fetch_assoc()){  
$_SESSION["post_row_id"] = $rows["id"]; #Storing post id for updation.
echo $_SESSION["post_row_id"];
?>         

        <tr class="gradeA">
            <td><?php echo ++$i; ?></td>

    <td><u><a onmouseover="this.style.color='blue'" onmouseout="this.style.color='black'" href="post_action.php">
    <?php echo $rows["post_title"]; ?></a></u></td>

        </tr>                                       
  <?php
    }

}
?>   

我想用 $_SESSION["post_row_id"] 将 row_id 转移到 post_action.php 页面,但是每次我单击锚标记时,都会转移最后一个 id到下一页。 但是当我回显 $_SESSION["post_row_id"] 时,所有唯一值都会显示出来。

注意我通过在锚标记中传递 id 来实现它的工作正常。但在 URL 中,值正在显示,任何人都可以更改该值并传递一些其他数据。会话在那个时候是安全的,所以我试图用会话来做到这一点

【问题讨论】:

  • 1.您需要在要处理的每个页面上添加session_start(); SESSION
  • href="post_action.php?row_id="
  • @AlivetoDie:我已经添加了那行代码。
  • @JYoThI:我想用会话变量来做。
  • @JasshhAndrews - 我认为根据您的要求,会话不应该工作。您需要做的是您可以使用发布请求或获取请求。但出于安全原因,您可以在提交页面上验证 ID。

标签: php mysql session


【解决方案1】:
To stick with your original code,
you can pass parameters on your <a href> tag, 
by adding something like this to your url,

<a href="post_action.php?rows_id=$rows_id"></a>

 if your goal is just to pass that id to another page,  you may remove the session since its no longer needed since the id is already passed on your url,


<?php session_start();?>
           <?php
        #php code to get values
        include_once "process/config.php";
        $sql = "select * from posts";
        static $i=0;
        $result = mysqli_query($conn,$sql);
        if($result->num_rows > 0){
        while($rows = $result->fetch_assoc()){  
        $_SESSION["post_row_id"] = $rows["id"]; #Storing post id for updation.
        echo $_SESSION["post_row_id"];
        ?>         

                <tr class="gradeA">
                    <td><?php echo ++$i; ?></td>

            <td><u><a onmouseover="this.style.color='blue'" onmouseout="this.style.color='black'" href="post_action.php">
            <?php echo $rows["post_title"]; ?></a></u></td>

                </tr>                                       
          <?php
            }

        }
        ?>   

【讨论】:

    【解决方案2】:

    根据您的要求,您需要传递当前行的唯一 ID,因此请使用 GET method. for security purpose validate it on server side 像这样使用它

    <a href="post_action.php?row_id=<?php echo $rows["id"]; ?>"<?php echo $rows["post_title"]; ?></a>
    

    注意:您可以将所有 id 存储在会话中。但是您无法找到点击了哪一行 @JasshhAndrews

    【讨论】:

      猜你喜欢
      • 2015-12-21
      • 1970-01-01
      • 2017-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-08
      相关资源
      最近更新 更多