squiblo Posted July 23, 2009 Share Posted July 23, 2009 i want some pages such as www.squiblo.com/profile.php to only be seen when a user is logged in, but if i type it into the url u can see the contents of the page <? session_start(); if(!$_SESSION['myusername']){ header("location:index.php"); } ?> <html> <body> Login Successful! </body> </html> what is the code so the page can only be seen by people that are currently logged in? Link to comment https://forums.phpfreaks.com/topic/167200-solved-login-pages/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 24, 2009 Share Posted July 24, 2009 You need to use full opening php tags <?php You need to use !isset() to avoid generating php errors that will fill up your error log file. And you must use an exit; statement after the header() redirect to prevent the remainder of the code on the page from being executed. <?php session_start(); if(!isset($_SESSION['myusername'])){ header("location:index.php"); exit; } ?> Link to comment https://forums.phpfreaks.com/topic/167200-solved-login-pages/#findComment-881573 Share on other sites More sharing options...
squiblo Posted July 24, 2009 Author Share Posted July 24, 2009 what am i doing wrong? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <?php session_start(); if(!isset($_SESSION['myusername'])){ header("location:index.php"); exit; } ?> <html> <head> go to www.squiblo.com/search.php to see the problem Link to comment https://forums.phpfreaks.com/topic/167200-solved-login-pages/#findComment-881579 Share on other sites More sharing options...
PFMaBiSmAd Posted July 24, 2009 Share Posted July 24, 2009 You cannot output ANY content to the browser before the session_start(). The DOCTYPE must be after the php code that contains the session_start() Link to comment https://forums.phpfreaks.com/topic/167200-solved-login-pages/#findComment-881581 Share on other sites More sharing options...
squiblo Posted July 24, 2009 Author Share Posted July 24, 2009 i have swapped them around but its still acting the same <?php session_start(); if(!isset($_SESSION['myusername'])){ header("location:index.php"); exit; } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> Link to comment https://forums.phpfreaks.com/topic/167200-solved-login-pages/#findComment-881584 Share on other sites More sharing options...
PFMaBiSmAd Posted July 24, 2009 Share Posted July 24, 2009 Are you sure you are setting $_SESSION['myusername'] on a page that has a session_start() statement on it and that sessions are working? Link to comment https://forums.phpfreaks.com/topic/167200-solved-login-pages/#findComment-881608 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.