Jump to content

How To Check If A Session Is Started Or Not?


murtz

Recommended Posts

Hi Guys..

 

Basically.. I want users to be unable to access pages in my website that require logging in. For example.. A user starts off at the INDEX page.. and when they log in.. they are transferred to the HOME page. But.. at the moment.. even if I dont log in.. I can type /home.php in my URL and still get there!

 

Im trying to use the ISSET function.. but doesnt seem to be working.. the page just stays on load!

 

I put this piece of code in my home page...

 

<?php
ini_set('session.gc_maxlifetime', 10);	
session_start();

if (!(isset($_SESSION['session_name']) && $_SESSION['session_name'] != '')) {
header ("Location: index.php");
}
else{
header ("Location: home.php");
}
exit;

?>

 

And this is where I make my session in the login code

 

$student_id=$_POST['pid'];
$password=$_POST['pass'];

$login=" SELECT * FROM student WHERE student_id= '$student_id' AND password='$password'";
$result=mysql_query($login);

//check for rows in the database
$count=mysql_num_rows($result);

if($count==1){
$_SESSION['session_name'] = $student_id;

 

Please help!

I tried the above suggestions.. but when Im on the index page.. and try and log in.. it just keeps loading.. doesnt move from the index page?

 

Basically.. the first page a user goes to is INDEX... and when they are logged in they are redirected to the HOME page.

 

 

But I dont need it to be displayed.. I just want any random to be unable to type in 'www.etcetc/HOME.PHP' on the browser and be taken to the page.

 

I want them to be redirected to the INDEX page where they have to log in first. Just helps make my site more secure.

Put this at the top of the page to be blocked unless logged in and see if this works.

 

<?php

session_start();

if (!isset($_SESSION['session_name'])) {

    header ("Location: index.php");

    exit;

}

echo "this works fine is sessions is working.";

 

?>

 

This is just a basic check.

 

the code that mark gave you will tell you if the session is working or not by echoing the session_id.  if you get a blank page, then your sessions arent working.

 

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.