hosseini Posted August 7, 2013 Share Posted August 7, 2013 (edited) hi all , I have to system running Ubuntu 13.04 . on my laptop everything works fine but on my PC I can not work with session variable , I have installed Apache2 and PHP from terminal ,i'm running a same code on both devices. the code that I used for checking if the variable is set or not on every page is : <?php session_start(); if(!isset($_SESSION["mys"])) { header('location:../index.php'); } ?> as you can see if the variable do not set , user would redirect to the index page . the problem is my session variable is not getting set at all , this is a code that i ran on both devices : <?php session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <link REL="SHORTCUT ICON" HREF="qeshmac.ico"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <?php include 'mytool.php'; if(isset($_POST["myusername"])) { // username and password sent from form $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; $host='localhost'; // Host name $username='root'; // Mysql username $password=''; // Mysql password switch ($_POST['tabs']) { case 'students': $db_name='students'; // Database name $tbl_name='test'; // Table name break; case'teachers': $db_name='managers'; // Database name $tbl_name='teachers'; // Table name break; case'managers': $db_name='managers'; // Database name $tbl_name='employes'; // Table name break; case'admin': $db_name='managers'; // Database name $tbl_name='moderator'; // Table name break; } // Connect to server and select database. mysql_connect($host,$username,$password)or die("cannot connect"); $selected=mysql_select_db($db_name)or die("cannot select DB"); mysql_query("SET CHARACTER SET utf8;"); mysql_query("SET SESSION collation_connection = 'utf8_persian_ci'"); $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); if(mysql_fetch_assoc($result)) { switch ($_POST['tabs']) { case 'students': $_SESSION["mys"]=$myusername; print( redirect('student/index.php')); break; case 'teachers': $_SESSION["mys"]=$myusername; print( redirect('teachers/index.php')); break; case 'managers': $_SESSION["mys"]=$myusername; print( redirect('managers/index.php')); break; case 'admin': $_SESSION["mys"]=$myusername; print( redirect('admin/index.php')); break; } } else { //back to login print( redirect('index.php?cmd=error')); } } else { print( redirect('index.php')); } ?> </body> </html> Edited August 7, 2013 by hosseini Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted August 7, 2013 Share Posted August 7, 2013 All this logic should come before any HTML output. I have cleaned up the code a bit. <?php session_start(); include 'mytool.php'; if(isset($_POST['myusername']) && isset($_POST['mypassword'])) { // username and password sent from form $myusername = $_POST['myusername']; $mypassword = $_POST['mypassword']; $host = 'localhost'; // Host name $username = 'root'; // Mysql username $password = ''; // Mysql password if(isset($_POST['tabs'])) { switch ($_POST['tabs']) { case 'students': $db_name = 'students'; // Database name $tbl_name = 'test'; // Table name $redirect = 'student/index.php'; break; case 'teachers': $db_name = 'managers'; // Database name $tbl_name = 'teachers'; // Table name $redirect = 'teachers/index.php'; break; case 'managers': $db_name = 'managers'; // Database name $tbl_name = 'employes'; // Table name $redirect = 'managers/index.php'; break; case'admin': $db_name = 'managers'; // Database name $tbl_name = 'moderator'; // Table name $redirect = 'admin/index.php'; break; } } else { die('Missing var: tabs'); } // Connect to server and select database. mysql_connect($host, $username, $password)or die("cannot connect"); $selected = mysql_select_db($db_name)or die("cannot select DB"); if(!$res = mysql_query("SET CHARACTER SET utf8;")) { die(mysql_error()); } if(!$res = mysql_query("SET SESSION collation_connection = 'utf8_persian_ci'")) { die(mysql_error()); } $sql = "SELECT * FROM " . $tbl_name . " WHERE username='" . $myusername . "' AND password='" . $mypassword . "'"; if(!$result = mysql_query($sql)) { die(mysql_error()); } if(mysql_num_rows($result)) { $row = mysql_fetch_assoc($result); // not sure why you are not using the pk from the result set in your session var $_SESSION['mys'] = $myusername; header('Location:' . $redirect); exit(); } else { //back to login header('Location:index.php?cmd=error'); exit(); } } else { header('Location:index.php'); exit(); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <link REL="SHORTCUT ICON" HREF="qeshmac.ico"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> </body> </html> Quote Link to comment Share on other sites More sharing options...
hosseini Posted August 7, 2013 Author Share Posted August 7, 2013 tnx for you help but your code is not working too I don't know what is the problem ! everything is ok but the session variable is not getting set ! the funny thing is my other project session variables are working but not this one ! Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted August 7, 2013 Share Posted August 7, 2013 do you have php's error_reporting set to E_ALL and display_errors set to ON so that php would help you with things like session_start() error? also, have you even determined that your code is taking the execution path you think it is and is setting the $_SESSION variable to a specific expected value? Quote Link to comment Share on other sites More sharing options...
hosseini Posted August 7, 2013 Author Share Posted August 7, 2013 do you have php's error_reporting set to E_ALL and display_errors set to ON so that php would help you with things like session_start() error? also, have you even determined that your code is taking the execution path you think it is and is setting the $_SESSION variable to a specific expected value? yes I have error reporting on my codes but this one as i told was working fine on my laptop and on my pc it's not working . I've found what was wrong . the local server on my ubuntu desktop didn't install properly , so I will reinstall it to see if it is gonna work ! Quote Link to comment Share on other sites More sharing options...
Solution hosseini Posted August 7, 2013 Author Solution Share Posted August 7, 2013 It was php.ini problem with session path and I solved it tnx in advance for your guys help Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.