i start a new session with : session_start();
then i set some session variable like this :
$_SESSION['name']=$_POST['name'];\
and some another variables.
at bottom of page i set header to diffrent page :
header('location: index.php');
exit();
now in new page (index.php i can't access to my session
variables, like $_SESSION['name'])
what's wrong ?
thanks.



here is my files :

a.php

========================================

session_start();

require ('config.inc.php');
if(isset($_POST))
foreach($_POST as $v=>$k)
{
$items[$v]=$k;
}

$sql="SELECT * FROM members WHERE
username='{$items['user']}' AND
pass=MD5('{$items['Password']}') ";
$res=mysql_query($sql);
$row=mysql_fetch_assoc($res);
if($row['username'])
{ $_SESSION['type']=$row['type'];
$_SESSION['name']=$row['name'];
$_SESSION['family']=$row['family'];
$_SESSION['username']=$row['username'];
$_SESSION['date']=$row['date'];

}

header('location: admin.php');
exit();

======================================

admin.php

=====================================

<?php
session_start();
if(!isset($_SESSION['admin']))
{
header('location: index.php');
exit();
}
require ('config.inc.php');

?>

.

.

.

=================================

Answers were Sorted based on User's Feedback



i start a new session with : session_start(); then i set some session variable like this : $_SES..

Answer / ravi

It may be possible you are not start $_SESSION() in index
page. Your code which are shown above is correct. Or may be
You closed session. So checked your code.

Is This Answer Correct ?    5 Yes 1 No

i start a new session with : session_start(); then i set some session variable like this : $_SES..

Answer / muthu

Ravis answer is correct. But sessions wont be closed. It
will be active till the browser closes. OR if($row
['username']) fails that session variable wont be created.
Better you should assign like $_SESSION['name']=''.

Is This Answer Correct ?    0 Yes 0 No

i start a new session with : session_start(); then i set some session variable like this : $_SES..

Answer / paras

if you have written session_start() in you index.php file
then surely it will work. If you can provide you index.php
file's code then we can answer it with better solution!

Is This Answer Correct ?    0 Yes 0 No

i start a new session with : session_start(); then i set some session variable like this : $_SES..

Answer / rahul prasad

Its not working because you have used "exit" at the end.
And it wont work even if you add session_start() at the
beginning.

Remove "exit" and it will work correctly.

Is This Answer Correct ?    0 Yes 0 No

i start a new session with : session_start(); then i set some session variable like this : $_SES..

Answer / jose martini

I created 4 files:
start.php
admin.php
index.php
sessionend.php

Code:
====================
<?php
/* File: start.php
*/
session_start();

$_SESSION['name']= "Jose";
$_SESSION['lastname']= "Martini";

echo "Start:" . $_SESSION['name'];

header('location: admin.php');

exit();
?>
=================
<?php
session_start();

echo "Admin";
if (isset ($_SESSION['name'])) {
header('location: index.php?'.$_SESSION['name']);
}
else {
echo "Admin: No Session";
}
exit ();

?>
================
<?php
/*
*/
session_start();

if (isset ($_SESSION['name'])) {
echo $_SESSION["name"];
}
else {
echo "Index: no Session";
}

exit();
?>
=======
<?php
/*
*/
session_start();
session_destroy();

echo "End Session";

?>
================

I tried to reproduce the error, the only way that the
session is not available is if you miss to start the session
or to initialized the session variable.

the exit() does not affect the result.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More PHP Interview Questions

How can we connect to a mysql database from a php script?

0 Answers  


Define anonymous classes in php7?

0 Answers  


Is php easy language to learn?

0 Answers  


What are the features and advantages of object-oriented programming in php?

0 Answers  


How do you measure variables?

0 Answers  


How can you get, the total size of a certain directory?

1 Answers  


How to remove white spaces from the beginning and/or the end of a string in php?

0 Answers  


How many ways we can give the output to a browser?

6 Answers  


hi, i have knowledge about PHP/MYSQL,i am fresher M.Sc-IT 2009 pass out,any recruitment in PHP please let me know.

0 Answers  


code to see the priview of the image which is being uploaded (after browising the image... just click priview ... how it will be visible...before uploading)

0 Answers   Satyam,


What is php data type?

0 Answers  


How to redirect https to http url and vice versa in .htaccess?

0 Answers  


Categories