session not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
shiranwas
Forum Commoner
Posts: 53
Joined: Fri Jul 07, 2006 10:41 pm
Location: Colombo

session not working

Post 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.
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

Hello dear,
maybe you forgot to call

Code: Select all

session_start()
function in director.php. The good way to check if session is working or not is by printing the supergloabl array by

Code: Select all

print_r($HTTP_SESSION_VARS);
Cheers,
Dibyendra
User avatar
shiranwas
Forum Commoner
Posts: 53
Joined: Fri Jul 07, 2006 10:41 pm
Location: Colombo

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

they're there, just empty values.
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post 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
User avatar
shiranwas
Forum Commoner
Posts: 53
Joined: Fri Jul 07, 2006 10:41 pm
Location: Colombo

Post 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>";			
			}
?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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'])){
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply