Page 1 of 1

Session object, session variable, HELPPPP

Posted: Mon Mar 17, 2008 11:39 am
by lepi
i am new in PHP,and ofcourse i have a problem :)
so,here is my problem
with first php i create session variable,but in sibling php i dont have it in session
here is my code

first.php

Code: Select all

<?header("Content-type: text/html");?>
<? session_start();
$_SESSION["myVariable"] = 1;?>
second.php

Code: Select all

<?header("Content-type: text/html");?>
<? 
$myVariable=$_SESSION["myVariable"];
if($myVariable==null) {
?>Session variable doesnt exist<?
}
else {
?>Session variable exist<?
}

Re: Session object, session variable, HELPPPP

Posted: Mon Mar 17, 2008 11:49 am
by scriptah
On second.php you should also call session_start( ) before any output.

Re: Session object, session variable, HELPPPP

Posted: Mon Mar 17, 2008 11:58 am
by lepi
thnx :)
i figured out by my self,but thnx on fast answer :)
i am java programmer,so i have a little problem with php
but my hosting site has only PHP,so.... :D

Re: Session object, session variable, HELPPPP

Posted: Mon Mar 17, 2008 11:58 am
by Zoxive
lepi wrote:first.php

Code: Select all

<?header("Content-type: text/html");?>
<? session_start();
$_SESSION["myVariable"] = 1;?>
->

Code: Select all

<?php
session_start();
//header("Content-type: text/html"); // Not Needed 
//
$_SESSION['myVariable'] = 1;
 
lepi wrote:second.php

Code: Select all

<?header("Content-type: text/html");?>
<? 
$myVariable=$_SESSION["myVariable"];
if($myVariable==null) {
?>Session variable doesnt exist<?
}
else {
?>Session variable exist<?
}
->

Code: Select all

<?php
session_start();
//header('Content-type: text/html'); // Not Needed
$myVariable = isset($_SESSION['myVariable'])? $_SESSION['myVariable'] : NULL;
if(!empty($myVariable)):?>
Session variable doesnt exist
<?php elseif: ?>
Session Variable Exists = <?php echo $myVariable; ?>
<?php endif; ?>
 
Alternate ->

Code: Select all

<?php
session_start();
//header('Content-type: text/html'); // Not Needed
$myVariable = isset($_SESSION['myVariable'])? $_SESSION['myVariable'] : NULL;
if(!empty($myVariable)){
  echo 'Session variable doesnt exist';
}else{
  echo 'Session variable exists = ' . $myVariable;
}