murtz Posted June 19, 2008 Share Posted June 19, 2008 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! Quote Link to comment Share on other sites More sharing options...
Stephen Posted June 20, 2008 Share Posted June 20, 2008 For the home page you could try putting: ini_set('session.gc_maxlifetime', 10); session_start(); if (!isset($_SESSION['session_name'])) { header ("Location: index.php"); } else { header ("Location: home.php"); } exit; Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted June 20, 2008 Share Posted June 20, 2008 Change your AND condition to an OR condition... This: if (!(isset($_SESSION['session_name']) && $_SESSION['session_name'] != '')) { should be: if (!(isset($_SESSION['session_name']) || $_SESSION['session_name'] != '')) { Quote Link to comment Share on other sites More sharing options...
murtz Posted June 20, 2008 Author Share Posted June 20, 2008 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. Quote Link to comment Share on other sites More sharing options...
mark110384 Posted June 20, 2008 Share Posted June 20, 2008 The following will display your session id. session_start(); $session = session_id(); echo $session; Quote Link to comment Share on other sites More sharing options...
murtz Posted June 21, 2008 Author Share Posted June 21, 2008 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. Quote Link to comment Share on other sites More sharing options...
peranha Posted June 21, 2008 Share Posted June 21, 2008 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. Quote Link to comment Share on other sites More sharing options...
murtz Posted June 21, 2008 Author Share Posted June 21, 2008 Nice one mate! It works now with that code! by the way.. I echoed the session.. and get something like 593cdf0f5adc2b9eae7b60eb3044107e .. does that seem right? Quote Link to comment Share on other sites More sharing options...
peranha Posted June 21, 2008 Share Posted June 21, 2008 yes that would be right with the echo. Quote Link to comment Share on other sites More sharing options...
murtz Posted June 21, 2008 Author Share Posted June 21, 2008 Thanks alot mate really appreciate it 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.