我对PHP代码有疑问。
我在我的第一页中创建了会话,我可以打印会话(在同一页面中说$_SESSION['cno']
但是当我试图在我的第二页中访问它时,它说cno(会话变量)是未定义的..
为什么这样........
我通过谷歌搜索阅读教程和答案,但没有任何作用 我已经在每个页面开始了会话........ 第1页
<?php
include("connection.php");
session_start();
if(isset($_POST['proceed']))
{
$user_name=$_POST['id2'];
$name=$_POST['id3'];
$spts = $_POST['id10'];
$carea = $_POST['id20'];
$ctype = $_POST['id13'];
$email = $_POST['email'];
$paddress=$_POST['id4'] ;
$cdat = $_POST['id7'];
$dis = $_POST['id9'];
$con = $_POST['id11'];
$cno = $_POST['cntct'];
$insert="INSERT INTO cregister(u_name,name,addrs,cnt_no,e_mail,cs_type,dt_ocrnc,pls_ocrnc,dscrptn,suspts,cnvts) VALUES('$user_name','$name','$paddress','$cno','$email','$ctype','$cdat','$carea','$dis','$spts','$con')";
mysql_query($insert);
$lastid = mysql_insert_id();
$_SESSION['cno']=$lastid;/////////////////////////// this is the created session
header("location:printform.php");
}
?>
2页
<?php
require "connection.php";
session_start();
$k = $_SESSION['cno'];//// this is the previously created session tried to re access
echo $k;
$query = mysql_query("SELECT * FROM cregister WHERE cno = '$k'");
$row =mysql_fetch_assoc($query);
$_SESSION['username'] = $row['u_name'];
$_SESSION['name'] = $row['name'];
$_SESSION['contact'] = $row['cnt_no'];
$_SESSION['email'] = $row['e_mail'];
$_SESSION['addrs'] = $row['addrs'];
$_SESSION['cstype'] = $row['cs_type'];
$_SESSION['dt'] = $row['dt_ocrnc'];
$_SESSION['pls'] = $row['pls_ocrnc'];
$_SESSION['dscrptn'] = $row['dscrptn'];
$_SESSION['suspts'] = $row['suspts'];
$_SESSION['cnvts'] = $row['cnvts'];
$file_to_delete = '../sdf.pdf';
unlink($file_to_delete);
//header("location:../pdfmaker.php");
?>
答案 0 :(得分:1)
您需要在第二页的开头开始会话(session_start()
)。否则,您无法访问$_SESSION
数组。