使用$ _SESSION来传输数据

时间:2011-10-28 01:48:57

标签: php mysql session

我试图在我正在创建的表单输入中使用$ _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');
  }
?>

任何人都可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:6)

您必须使用session_start()

在脚本的开头启动会话

答案 1 :(得分:1)

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