This is my Index.php file which won't get me to any other page
<?php
session_start();
include_once 'dbconnect.php';
//Connecting to the database
if(!isset($_SESSION['user'])){
header("Location: Login.php");
}
//If the user is not logged in, this code will redirect them to the Login page
$res=mysql_query("SELECT * FROM users WHERE UserID=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
/*This is finding all the data based on the User's User ID.
This is very safe as it will only find the data relevant to the User that is logged in */
?>
Welcome -
<h1>Welcome <?php echo $userRow['Username']; ?> to our website</h1>
<!-- This here is doing the same thing as before -->
<a href="Logout.php?logout"> Log Out</a> | <a href="MySubjects.php"> My Subjects</a> |
<a href = "NewSubject.php"> Add a new subject</a>
<a href = "Test.php"> Testing</a>
2.This is my NewSubject.php file. Both this and MySubjects.php are similar to each other
<?php
require_once('Dbconnect.php');
//Connecting to the database
if(!isset($_SESSION['user'])){
header("Location: Login.php");
}
//User is being redirected to the login page if they are not logged in.
else{
$res=mysql_query("SELECT * FROM subjects WHERE UserID=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
}
//This is fetching the data based on the Session Variable UserID
?>
</head>
<title> Add a new subject</title>
<body>
<form method = "Post">
<select name = SubjectName>
<option value="Maths">Maths</option>
<option value="FurtherMaths">Further Maths</option>
<option value="EnglishLanguage">English Language</option>
<option value="EnglishLiterature">Engliash Literature</option>
</select>
</form>
<a href="Logout.php?logout"> Log Out</a> | <a href="My Subjects.php"> My Subjects</a> |
<a href = "NewSubject.php"> Add a new subject</a>
</body>
THIS IS A TEST!
I did a lot of research and I have found the answer to what is going wrong in my code. The problem is that at the start of each of my page I have forgotten to write the following: session_start();
this means, everytime I try to open the link, it doesn't follow the if(!isset($_SESSION['user']))
because it doesn't exist in that page (as there is no session variable set).
Therefore, when I wrote session_start();, it worked perfectly!