【问题标题】:Using $_SESSION to carry data使用 $_SESSION 携带数据
【发布时间】:2011-10-28 01:48:57
【问题描述】:

我试图在我正在创建的表单输入中使用 $_SESSION,但是我无法让它工作并且不知道我做错了什么,当将数据传递到下一个表单时,它可以与表单的前一部分一起使用页面 - 但是代码似乎不适用于表单的主要部分。

    <?php

//This includes the variables, adjusted within the 'config.php file' and the functions from the 'functions.php' - the config variables are adjusted prior to anything else.
require('configs/config.php');
require('configs/functions.php');

//Check to see if the form has been submited, if it has we continue with the script.
if(isset($_POST['confirmation']) && isset($_POST['name']) && isset($_POST['email']) && isset($_POST['address1']) && isset($_POST['city']) && isset($_POST['postcode']) and $_POST['confirmation']=='true')
{
    //Slashes are removed, depending on whether magic_quotes_gpc is on.
    if(get_magic_quotes_gpc())
    {
        $_POST['name'] = stripslashes($_POST['name']);
        $_POST['email'] = stripslashes($_POST['email']);
        $_POST['address1'] = stripslashes($_POST['address1']);
        $_POST['address2'] = stripslashes($_POST['address2']);
        $_POST['city'] = stripslashes($_POST['city']);
        $_POST['postcode'] = stripslashes($_POST['postcode']);
        $_POST['phonenum'] = stripslashes($_POST['phonenum']);
    }

    //Create the future reference number of the repair.
    $maxid = mysql_fetch_array(mysql_query('select max(id) as id from repairs'));
    $id = intval($maxid['id'])+1;

    //Create the future reference number of the repair.
    $maxref = mysql_fetch_array(mysql_query('select max(reference) as reference from repairs'));
    $reference = intval($maxref['reference'])+8;

    //Here the session variables are converted back into standard variables.
    $model = $_SESSION['model'];
    $problem = $_SESSION['problem'];
    $info = $_SESSION['info'];
    $device = $_SESSION['device'];
    $price = $_SESSION['price'];
    $image = $_SESSION['image']; 

    //Here the variables are protected using mysql_real_escape_string.
    $name = mysql_real_escape_string(substr($_POST['name'],0,150));
    $email = mysql_real_escape_string(substr($_POST['email'],0,255));
    $address1 = mysql_real_escape_string(substr($_POST['address1'],0,255));
    $address2 = mysql_real_escape_string(substr($_POST['address2'],0,255));
    $city = mysql_real_escape_string(substr($_POST['city'],0,100));
    $postcode = mysql_real_escape_string(substr($_POST['postcode'],0,9));
    $phonenum = mysql_real_escape_string(substr($_POST['phonenum'],0,11));
    $date = date("r");

    //Here the variables are protected using trim.
    $name = trim($name);
    $email = trim($email);
    $address1 = trim($address1);
    $address2 = trim($address2);
    $city = trim($city);
    $postcode = trim($postcode);
    $phonenum = trim($phonenum);

    //Here the variables are protected using htmlspecialchars.
    $name = htmlspecialchars($name);
    $email = htmlspecialchars($email);
    $address1 = htmlspecialchars($address1);
    $address2 = htmlspecialchars($address2);
    $city = htmlspecialchars($city);
    $postcode = htmlspecialchars($postcode);
    $phonenum = htmlspecialchars($phonenum);

    //Here the variables are protected using strip_tags.
    $name = strip_tags($name);
    $email = strip_tags($email);
    $address1 = strip_tags($address1);
    $address2 = strip_tags($address2);
    $city = strip_tags($city);
    $postcode = strip_tags($postcode);
    $phonenum = strip_tags($phonenum);

    //The details about the repair are entered into the database
    $query = mysql_query("insert into repairs (id, model, problem, info, name, email, address1, address2, city, postcode, phonenum, price, date, reference) values ('$id', '$model', '$problem', '$info', '$name', '$email', '$address1', '$address2', '$city', '$postcode', '$phonenum', '$price', '$date', '$reference')") or die(header('Location: 404.php'));
?>

这里有一些 HTML。

<?  
  }
  else {
     header('Location: 404.php');
  }
?>

谁能帮我解决这个问题?

【问题讨论】:

  • 你似乎没有在任何地方打电话给session_start()
  • 页面中是否包含session_start()
  • 我不明白你的问题。什么不在这里工作?表格?表格处理? SQL?会话存储?你得到什么样的错误?如果是会话问题,请记住您需要在页面顶部开始会话 session_start();
  • 会话已经在前一页被调用,我没有取消设置或销毁它,即使它被调用也没有区别。无论如何,会话似乎在没有调用它的情况下继续上一页。
  • session_start();如果您需要使用它,必须在每个页面上。

标签: php mysql session


【解决方案1】:

您必须在脚本开头使用session_start() 启动会话

【讨论】:

  • 我终于让它工作了,问题是我不小心(并且愚蠢地)将脚本移动到另一个文件夹而我没有注意到。还有 session_start();不需要在属于单个会话的每个页面上调用。
  • 是的,如果session.auto_start 已经在PHP 配置中定义了true,那么没有。即使这样,您也应该养成一个好习惯,至少出于兼容性原因,总是在每个页面上调用它。如果一个会话存在,另一个session_start() 将被忽略,所以没有理由不这样做。此外,您的代码中有两行 require 行,所以我想知道您如何能够将此脚本移动到另一个文件夹而没有注意到它会导致致命的 E_COMPILE_ERROR.
【解决方案2】:

将您的错误记录设置为最详细的级别。如果您的粘贴是准确的,那么您在开头有一些空格会导致您无法再发送标头,因此您无法启动会话。

【讨论】:

    猜你喜欢
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多