Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP Members view.

Featured Replies

Hey :D

 

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.

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.

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; ?>

  • 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.

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.

  • 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>

You have a bracket missing here:

 

<?php if(session_is_registered(myusername) :?>

 

should be

 

<?php if(session_is_registered(myusername)) :?>

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.

  • 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 :D

 

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..

Yep that worked, Thanks :D

 

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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.