为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

婷婷同学_ 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 关注
  • 34 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信