Page 1 of 1
session not working
Posted: Thu Aug 17, 2006 12:20 am
by shiranwas
i have page called loginto.php start session on the top of the page before anythig as follows
Code: Select all
<?php
if (!isset($_SESSION['userlevel'])){
session_start("mertek");
$_SESSION['userid'] = '';
$_SESSION['userlevel'] = '';
$_SESSION['deptid'] = '';
$_SESSION['subdept'] = '';
}
?>
and when the user input the password and the user name i assign values to all session variables if the user name and password are correct and its direct to a new page i print all the session variables in the new page called complaint.php it works fine,but i when i direct to another page called director.php through a hyperlink which is in the complaint.php nothing prints. I tried
Code: Select all
if (!isset($_SESSION['userid'] ) ){
echo "$_SESSION['userid'] ";
echo "$_SESSION['userlevel'] ";
echo "$_SESSION['deptid'] ";
echo "$_SESSION['subdept'] ";
}else{
echo "not set";
}
also , then it reurns "not set" what is the problam here,
can any one kindly help me to solve this problam.
Posted: Thu Aug 17, 2006 12:33 am
by dibyendrah
Hello dear,
maybe you forgot to call
function in director.php. The good way to check if session is working or not is by printing the supergloabl array by
Cheers,
Dibyendra
Posted: Thu Aug 17, 2006 12:40 am
by shiranwas
hi,
i put the session start on the top and tried with your code then it returns(printed)
Array ( [userid] => [userlevel] => [deptid] => [subdept] => ) >
i think it still not carring values for the session variable.
what is the problam.
thanks
shiran
Posted: Thu Aug 17, 2006 12:47 am
by feyd
they're there, just empty values.
Posted: Thu Aug 17, 2006 1:14 am
by dibyendrah
hello dear,
Session is working well. The only thing is that you're not assigning any values to the variables. Just assign some values in your code and use the print_r() to see if they are assigned or not.
Cheers,
Dibyendra
Posted: Thu Aug 17, 2006 1:23 am
by shiranwas
i assign values in first page loginto.php, but not in the page director.php
Code: Select all
<?php
// connecting to the database
include("Conn.php");
$selected=mysql_select_db("complaints")
or die("Could not connect to DB");
//_____________________________________________________________________________
// Checking for the button ________________________________________________
$err="";
$USER=$_POST['username'];
$PASS=$_POST['password'];
//echo $USER;
//echo $PASS;
if ($USER!=""){
$query = "SELECT * FROM tblUsers where username ='$USER' and password ='$PASS'";
$result=mysql_query($query) or die("error");
if (!$result)
{
die ("Could not query the database: <br />". mysql_error()); }
//MYSQL_NUM can use instead MYSQL_ASSOCthen use $row[0]
$row = mysql_fetch_array($result, MYSQL_ASSOC) ;
if ( $row[username]==$USER and $row[password]==$PASS){
$_SESSION['userid'] = $row[userid];
$_SESSION['userlevel'] =$row[levelid];
//$_SESSION['deptid'] ='001';
$_SESSION['subdept'] = $row[deptsubid];
include("complaint.php");
} else{
$err="Access denied, Check your user name & password";
include("loginto.php");
echo "<div id='Layer1' style='position:absolute; left:50px; top:200px; width:600px; height:111px; z-index:1'> ";
echo "<label name='errorMess'>$err</label> ";
echo "</div>";
}
mysql_close($dbh);
//}//__________________________________________________________________________
}else{
$err="Access denied, user name can't be empty";
include("loginto.php");
echo "<div id='Layer1' style='position:absolute; left:50px; top:200px; width:600px; height:111px; z-index:1'> ";
echo "<label name='errorMess'>$err</label> ";
echo "</div>";
}
?>
Posted: Thu Aug 17, 2006 2:27 am
by RobertGonzalez
This is wrong. Change this...
Code: Select all
if (!isset($_SESSION['userlevel'])){
session_start("mertek");
to this
Code: Select all
session_start("mertek");
if (!isset($_SESSION['userlevel'])){
Posted: Thu Aug 17, 2006 8:02 am
by feyd
..and make sure to quote your named array indices.
Code: Select all
if ( $row[username]==$USER and $row[password]==$PASS){
$_SESSION['userid'] = $row[userid];
$_SESSION['userlevel'] =$row[levelid];
//$_SESSION['deptid'] ='001';
$_SESSION['subdept'] = $row[deptsubid];
Five examples of no quotes.