无法同时存储会话值和重定向页面

我的目的是存储会话值并将其传递给我的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">

婷婷同学_
浏览 88回答 1
1回答

慕沐林林

您有一些需要解决的问题,但我认为主要问题是,当您重定向时,您的帖子不再设置,因此您只想直接发布到目标页面:<form role="form" action="loadpage.php" method="POST" name="theForm" id="theForm">然后在该页面上执行会话操作。另外,我会创建一个函数来获取您的请求,以便您可以进行一些过滤:<?php# You could include this from a separate page, makes your script cleanerfunction getRequest($key = false, $type = 'post'){&nbsp; &nbsp; switch($type) {&nbsp; &nbsp; &nbsp; &nbsp; case('get'):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $arr = $_GET;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case('post'):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $arr = $_POST;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $arr = $_REQUEST;&nbsp; &nbsp; }&nbsp; &nbsp; if(!empty($key))&nbsp; &nbsp; &nbsp; &nbsp; return (isset($arr[$key]))? trim($arr[$key]) : null;&nbsp; &nbsp; return $arr;}# Don't put this after any "if" statements, just make it first thing on top of# every top-level pagesession_start();# Just stop if invalidif(empty(getRequest('selectedPage')))&nbsp; &nbsp; 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 runswitch(getRequest('selectedPage')) {&nbsp; &nbsp; case "page_0":&nbsp; &nbsp; &nbsp; &nbsp; # Since everything else redirects, just die here&nbsp; &nbsp; &nbsp; &nbsp; die("Konu Seçmediniz.");&nbsp; &nbsp; case "page_1":&nbsp; &nbsp; &nbsp; &nbsp; $page = "page2_form";&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case "page_2":&nbsp; &nbsp; &nbsp; &nbsp; $page = "page_2";&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; $page = "page1_form";&nbsp; &nbsp; &nbsp; &nbsp; break;}# Just do one redirect hereheader("Location: {$page}.php");# This may be the end of the script anyway, but best to get in the habit of# exiting after redirectexit;如果您确实想先发布到同一页面然后重定向,则在您的 loadpage.php 上您需要使用会话值而不是 $_POST 进行重定向值:<?phpswitch($_SESSION['selectedPage']) {&nbsp; &nbsp; case "page_0":&nbsp; &nbsp; &nbsp; &nbsp; # Since everything else redirects, just die here&nbsp; &nbsp; &nbsp; &nbsp; die("Konu Seçmediniz.");&nbsp; &nbsp; case "page_1":&nbsp; &nbsp; &nbsp; &nbsp; $pg = "page2_form";&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case "page_2":&nbsp; &nbsp; &nbsp; &nbsp; $pg = "page_2";&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; $pg = "page1_form";&nbsp; &nbsp; &nbsp; &nbsp; break;}# Just do one redirect hereheader("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&nbsp; may mess you# up down the road.
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5