September 30, 201015 yr Hey Just wondering if this is possible in PHP. I have a very basic login script which I made from this tutorial which was part of a blog tutorial someone suggested for me. I was wondering if I could have a piece of PHP code which shows only certain parts of a page depending whether I am logged in or not. I wanted it so it shows the login box when I am not logged in, and when I am it's replaced with text/links, also for it to make an edit link appear under each post. I know this can be done in asp.net 4, not sure about php though. Thanks in advance.
September 30, 201015 yr I have seen this done before using php and includes. So if the user is logged in it will show the included php file? I will see if i can dig it out.
September 30, 201015 yr That tutorial is quite old, and well - awful. It seems quite open to session jacking. I would suggest looking into classes for logins/users, and database handled sessions (over cookies). These will add tighter security to your site. Once you have built a simple login and/or user class, you can have a variable called 'is_logged_in' for example, then use the code: <?php if ($user->is_logged_in) :?> All your code for logged in users here <?php else :?> All your code for guests here <?php endif; ?>
September 30, 201015 yr Author I have seen this done before using php and includes. So if the user is logged in it will show the included php file? I will see if i can dig it out. Alright thanks and yeah, it's just for a login box and a link underneath each post.
September 30, 201015 yr If you want something quick & easy (and insecure): <?php if(session_is_registered(myusername) :?> Code for logged in users <?php else :?> Code for logged out users <?php endif; ?> That should work with your current script, assuming you copied the tutorial exactly.
September 30, 201015 yr Author If you want something quick & easy (and insecure): <?php if(session_is_registered(myusername) :?> Code for logged in users <?php else :?> Code for logged out users <?php endif; ?> That should work with your current script, assuming you copied the tutorial exactly. I copied the tutorial exactly, but I can't seem to get it working. I'm currently using a login page, but my aim is to get rid of that I am using it right <html> <title>ChrisOliver | Login</title> <head> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body> <div id="login"> <?php if(session_is_registered(myusername) :?> Code for logged in users <?php else :?> <div class="title"><h1>Login</h1></div> <div class="loginbox"> <table width="300" border="0" align="center" cellpadding="0" cellspacing="1"> <tr> <form name="form1" method="post" action="checklogin.php"> <td> <table border="0" cellpadding="3" cellspacing="1" style="width: 91%"> <tr> <td colspan="3"><strong><h1 align="center"></h1> </strong></td> </tr> <tr> <td style="width: 84px">Username</td> <td style="width: 8px">:</td> <td width="294"><input name="myusername" type="text" id="myusername"></td> </tr> <tr> <td style="width: 84px">Password</td> <td style="width: 8px">:</td> <td><input name="mypassword" type="password" id="mypassword"></td> </tr> <tr> <td style="width: 84px"> </td> <td style="width: 8px"> </td> <td><input type="submit" name="Submit" value="Login"></td> </tr> </table> </td> </form> </tr> </table> </div> <?php endif; ?> </div> </body> </html>
September 30, 201015 yr You have a bracket missing here: <?php if(session_is_registered(myusername) :?> should be <?php if(session_is_registered(myusername)) :?>
September 30, 201015 yr Yup, what mrsminkie said - my fault sorry. Also, that function has been deprecated which may be causing you issues on PHP 5.3 onwards. More info here: http://php.net/manual/en/function.session-is-registered.php I think you need to follow a more up-to-date tutorial - it turns out 'session_register' has also been deprecated, and as I said - the tutorial preaches pretty awful coding practice.
September 30, 201015 yr Author You have a bracket missing here: <?php if(session_is_registered(myusername) :?> should be <?php if(session_is_registered(myusername)) :?> Yep that worked, Thanks but I need a session_start I think.. This is my current one, If i am not logged in, it will redirect me to the login page What should it be instead? <? session_start(); if(!session_is_registered(myusername)){ header("location:login.php"); } ?> EDIT: Opps..Just figured it out..
September 30, 201015 yr Yep that worked, Thanks but I need a session_start I think.. This is my current one, If i am not logged in, it will redirect me to the login page What should it be instead? <? session_start(); if(!session_is_registered(myusername)){ header("location:login.php"); } ?> EDIT: Opps..Just figured it out.. Also use <?php not <? short tags may be disabled on some host
Create an account or sign in to comment