doujiao1180 2011-10-28 01:48
浏览 39
已采纳

使用$ _SESSION来传输数据

I have attempted to use $_SESSION in a form input I am creating however I cannot get it to work and do not know what I am doing wrong, it works with my previous part of the form when carrying data over to the next page - however the code does not seem to work for the main part of the form.

    <?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'));
?>

Some HTML is here.

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

Can anyone help me to get this to work?

  • 写回答

2条回答 默认 最新

  • doukan1258 2011-10-28 01:51
    关注

    You have to initiate your session in the beginning of your script with session_start()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀