Jump to content

Sessions Issue


jazzman

Recommended Posts

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 year
session_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]
Link to comment
https://forums.phpfreaks.com/topic/22899-sessions-issue/
Share on other sites

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...
Link to comment
https://forums.phpfreaks.com/topic/22899-sessions-issue/#findComment-103308
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.