user3474213
user3474213

Reputation: 43

Session undefined error

i have a doubt in php code. i created session in my first page and i could print the session( say $_SESSION['cno'] in the same page but when i tried to access it in my 2nd page it says that cno (session variable) is undefined.. Why is this so........

i read the tutorials and answers by googling but nothing works i have started the session in each page........ page1

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

}

?>

page2

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





?>

Upvotes: 0

Views: 296

Answers (1)

mesutozer
mesutozer

Reputation: 2859

You need to start session (by session_start()) in the beginning of the second page. Otherwise, you cannot access $_SESSION array.

Upvotes: 1

Related Questions