kinks Posted August 6, 2009 Share Posted August 6, 2009 So, I have all of my user stuff set-up, login, register, databases, tables, etc, etc. How do I do a simple PHP code or script that disables page view if not logged in? Quote Link to comment https://forums.phpfreaks.com/topic/169128-disabling-page-view-if-not-logged-in/ Share on other sites More sharing options...
mikesta707 Posted August 6, 2009 Share Posted August 6, 2009 if(!isset($_SESSION['my_session_variable'])){ die("YOU AINT LOGGED IN"); } include that on pages you want to protect Quote Link to comment https://forums.phpfreaks.com/topic/169128-disabling-page-view-if-not-logged-in/#findComment-892349 Share on other sites More sharing options...
kinks Posted August 6, 2009 Author Share Posted August 6, 2009 if(!isset($_SESSION['my_session_variable'])){ die("YOU AINT LOGGED IN"); } include that on pages you want to protect Thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/169128-disabling-page-view-if-not-logged-in/#findComment-892354 Share on other sites More sharing options...
plodos Posted August 6, 2009 Share Posted August 6, 2009 <?php session_start(); if ($_SESSION['loggedIn'] != 'admin') { //check the registered user, if not go index.php if(!$_SESSION['id']) { header("location:index.php"); die(); } else { echo " <script language='JavaScript'>history.go(-1);</script>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/169128-disabling-page-view-if-not-logged-in/#findComment-892513 Share on other sites More sharing options...
kinks Posted August 7, 2009 Author Share Posted August 7, 2009 I used <?php session_start(); if(!isset($_SESSION['my_session_variable'])){ die("You cannot view this page, you are not logged in!"); } ?> BUT, it displays the message for logged in users as well. Quote Link to comment https://forums.phpfreaks.com/topic/169128-disabling-page-view-if-not-logged-in/#findComment-892670 Share on other sites More sharing options...
The Little Guy Posted August 7, 2009 Share Posted August 7, 2009 OK, when a user logs in, set a variable, along with all the other variables you set, such as username, id, email, etc.: $_SESSION['logged'] = TRUE; Next, whenever you want to hide/disable something, or do something for non logged in users, do this, here are some examples: Redirect to login page: session_start(); if(!$_SESSION['logged']){ header("Location: login.php"); exit; } Display a login form or a user navigation bar: session_start(); if(!$_SESSION['logged']){ echo '<form action="login.php" method="post"> <p><input type="text" value="Username" name="username" /></p> <p><input type="password" name="password" /></p> </form>'; }else{ echo '<a href="home.php">Home</a> | <a href="logout.php">Logout</a>'; } Quote Link to comment https://forums.phpfreaks.com/topic/169128-disabling-page-view-if-not-logged-in/#findComment-892689 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.