亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法同時存儲會話值和重定向頁面

無法同時存儲會話值和重定向頁面

婷婷同學_ 2023-12-19 16:14:52
我的目的是存儲會話值并將其傳遞給我的page2_form.php。在將會話添加到我的代碼之前,我能夠將用戶定向到頁面,無論他/她從選擇->選項中選擇什么。我的 form 操作是 loadpage.php,每個選項都有一個 switch case,因為每個選項都是不同的形式(我現在只有一個,但稍后會添加更多)。添加會話變量后,我添加了 header('Location: loadpage.php'); 但它對我不起作用。它保存會話值但不引導我的頁面。我嘗試將表單操作更改回 header('Location: loadpage.php'); 但隨后我無法存儲會話值。我是 php/html 的新手,仍在學習,但找不到解決方案。如果需要,我可以發布更多代碼。page1_form.php    <?php      if(isset($_POST['ilerleButon'])){        session_start();        $_SESSION['sayi'] = htmlentities($_POST['sayi']);        $_SESSION['madde'] = htmlentities($_POST['madde']);        $_SESSION['tarih'] = htmlentities($_POST['tarih']);        $_SESSION['selectedPage'] = htmlentities($_POST['selectedPage']);        header('Location: loadpage.php');      }    ?>    <!DOCTYPE html>    <html>      <head>        <!-- Gerekli Meta Tagleri -->        <meta charset="utf-8">        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
查看完整描述

1 回答

?
慕沐林林

TA貢獻2016條經驗 獲得超9個贊

您有一些需要解決的問題,但我認為主要問題是,當您重定向時,您的帖子不再設置,因此您只想直接發布到目標頁面:


<form role="form" action="loadpage.php" method="POST" name="theForm" id="theForm">

然后在該頁面上執行會話操作。另外,我會創建一個函數來獲取您的請求,以便您可以進行一些過濾:


<?php

# You could include this from a separate page, makes your script cleaner

function getRequest($key = false, $type = 'post')

{

    switch($type) {

        case('get'):

            $arr = $_GET;

            break;

        case('post'):

            $arr = $_POST;

            break;

        default:

            $arr = $_REQUEST;

    }

    if(!empty($key))

        return (isset($arr[$key]))? trim($arr[$key]) : null;


    return $arr;

}

# Don't put this after any "if" statements, just make it first thing on top of

# every top-level page

session_start();

# Just stop if invalid

if(empty(getRequest('selectedPage')))

    die("Invalid request.");

# Assign the session variables

$_SESSION['sayi'] = htmlentities(getRequest('sayi'));

$_SESSION['madde'] = htmlentities(getRequest('madde'));

$_SESSION['tarih'] = htmlentities(getRequest('tarih'));

$_SESSION['selectedPage'] = htmlentities(getRequest('selectedPage'));

# Modify the switch a bit. What you have is not wrong though.

# Make sure to use exit after he redirect so the script doesn't continue to run

switch(getRequest('selectedPage')) {

    case "page_0":

        # Since everything else redirects, just die here

        die("Konu Se?mediniz.");

    case "page_1":

        $page = "page2_form";

        break;

    case "page_2":

        $page = "page_2";

        break;

    default:

        $page = "page1_form";

        break;

}

# Just do one redirect here

header("Location: {$page}.php");

# This may be the end of the script anyway, but best to get in the habit of

# exiting after redirect

exit;

如果您確實想先發布到同一頁面然后重定向,則在您的 loadpage.php 上您需要使用會話值而不是 $_POST 進行重定向值:


<?php

switch($_SESSION['selectedPage']) {

    case "page_0":

        # Since everything else redirects, just die here

        die("Konu Se?mediniz.");

    case "page_1":

        $pg = "page2_form";

        break;

    case "page_2":

        $pg = "page_2";

        break;

    default:

        $pg = "page1_form";

        break;

}

# Just do one redirect here

header("Location: {$pg}.php");

exit;

# If this is the end of the script, don't close it with a "?>" it is proper syntax to leave it off

# and then you don't have to worry about any hidden spaces that  may mess you

# up down the road.


查看完整回答
反對 回復 2023-12-19
  • 1 回答
  • 0 關注
  • 129 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號