jazzman Posted October 3, 2006 Share Posted October 3, 2006 Hi!I am running an Apache server on CentOS. My main page is in php. It's supposted to be a user sign-up page for teachers and students that want to access information found in the MySQL database on my server. I decided to use sessions in order to take care of the user verification, but I'm having some problems achieving the results I want.Here is the page code (don't notice the styling since it's using css):<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><?php//start session of a yearsession_save_path("sessions");ini_set("session.cookie_lifetime",time()+60*60*24*365);session_start();setcookie(session_name(), session_id(), time()+60*60*24*365, "/"); $input_id_student = $_POST['id_student']; $input_id_teacher = $_POST['id_teacher']; $input_pass = $_POST['pass']; $student = 0; $teacher = 0; $connection = mysql_connect("localhost", "", "") or die (mysql_error()); mysql_select_db("tagma_ora", $connection) or die (mysql_error()); mysql_query("SET character_set_results=utf8"); //check if the id is of a student $result = mysql_query("SELECT id FROM students", $connection); $num_rows = mysql_num_rows($result); for($i=0;$i<$num_rows;$i++){ $result_id[$i] = mysql_result($result,$i,"id"); if ($result_id[$i] == $input_id_student) { $student = 1; $result2 = mysql_query("SELECT first_name, last_name, birth_date, stream, grade, class FROM students WHERE id='$input_id_student'"); $first_name = mysql_result($result2,0,"first_name"); $last_name = mysql_result($result2,0,"last_name"); $birth_date = mysql_result($result2,0,"birth_date"); $birth_date2 = strtotime($birth_date); $birth_date3 = date('d/m/Y', $birth_date2); $stream = mysql_result($result2,0,"stream"); $grade = mysql_result($result2,0,"grade"); $class = mysql_result($result2,0,"class"); }} //check if the id is of a teacher $result = mysql_query("SELECT id FROM teachers", $connection); $num_rows = mysql_num_rows($result); for($i=0;$i<$num_rows;$i++){ $result_id[$i] = mysql_result($result,$i,"id"); if ($result_id[$i] == $input_id_teacher) { $teacher = 1; $result2 = mysql_query("SELECT first_name, last_name, password FROM teachers WHERE id='$input_id_teacher'"); $first_name = mysql_result($result2,0,"first_name"); $last_name = mysql_result($result2,0,"last_name"); $result_password = crypt(mysql_result($result2,0,"password")); }} if (strlen($input_id_student) == 0 && strlen($input_id_teacher) == 0 && strlen($input_pass) == 0) $_SESSION['type'] = "none"; else if ($student == 1) $_SESSION['type'] = "student"; else if ($teacher == 1 && crypt($input_pass, $result_password) == $result_password) $_SESSION['type'] = "teacher"; else if ($teacher == 1 && crypt($input_pass, $result_password) != $result_password) $_SESSION['type'] = "wrong_password"; else if (!isset($_SESSION['type'])) $_SESSION['type'] = "none"; else $_SESSION['type'] = "not_in_list"; ?><html lang="en" dir="ltr"> <head> <title>Tagma Ora</title> <link rel="stylesheet" type="text/css" href="style.css"> <link rel="shortcut icon" href="tagma_ora.png" type="image/x-icon"> </head> <body> <a href="index.php"><img src="tagma_ora.png"></a> <div id="div1"> <ul> <li><a href="index.php">Main Menu</a></li> <?php if ($_SESSION['type'] == "student" || $_SESSION['type'] == "teacher") echo '<li><a href="exit.php">exit</a></li>' ?> </ul> </div> <div id="dev2"><a href="index.php">Main Menu</a><hr width="98%"> <?php if ($_SESSION['type'] == "student") echo $first_name . ' ' . $last_name . '<br>Details:<br>Birth Date:' . $birth_date3 . '<br>Stream' . $stream . '<br>Class' . $grade2 . ' ' . $class; else if ($_SESSION['type'] == "teacher") echo 'Welcome teacher ' . $first_name . ' ' . $last_name; else if ($_SESSION['type'] == "wrong_password") echo 'Wrong passoword'; else if ($_SESSION['type'] == "none") { echo ' <span class="header">Welcome to your school database!</span><br> <br> <span class="emphasize">Students:</span><br> <form action="index.php" method="post">ID: <input name="id_student" type="text" maxlength="9"><br> <input type="submit" value="OK"> <br><br> <span class="emphasize">Teachers</span><br> <table rows="2" cols="2"> <tr><td>ID: </td><td><input name="id_teacher" type="text" maxlength="9"><br></tr> <tr><td>Password: </td><td><input name="pass" type="password"></td></tr></table><br><br> <input type="submit" value="OK"> </form>'; } else echo 'You are not in the database...'; ?></div> </body></html>[b]Now here's the precise problem[/b]:Whenever I sign-up as a student/teacher the session works and the page reloads showing "Welcome student...", HOWEVER, when I click on a hyperlink "back" to the main page the session changes back to "none" (meaning it shows the sign-up page). [b]How do I make the session stay even though I reload the page?[/b] Quote Link to comment https://forums.phpfreaks.com/topic/22899-sessions-issue/ Share on other sites More sharing options...
printf Posted October 3, 2006 Share Posted October 3, 2006 You can start a session or set a cookie if your sending output first, unless you use the ob_...() functions family! I good idea is turn error reporting on, so you see these errors and the other ones that have to do with your control logic!me! Quote Link to comment https://forums.phpfreaks.com/topic/22899-sessions-issue/#findComment-103271 Share on other sites More sharing options...
Daniel0 Posted October 3, 2006 Share Posted October 3, 2006 As printf said, you can't output and then send headers, you do not need to use output buffering though, just move the doctype declaration below your PHP code block. Quote Link to comment https://forums.phpfreaks.com/topic/22899-sessions-issue/#findComment-103276 Share on other sites More sharing options...
jazzman Posted October 3, 2006 Author Share Posted October 3, 2006 Thanks for the quick replies...I tried moving the doctype decleration to the bottom of the page but this didn't seem to work. Even removing it from the page still didn't have any effect.Regarding the ob_ functions... I tried putting ob_start(); at the first line after <?php and ob_end_flush(); before ?> but this still didn't have any effect on the results. Perhaps you could explain more specificly how I am supposed to alter the code... Quote Link to comment https://forums.phpfreaks.com/topic/22899-sessions-issue/#findComment-103308 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.