Skip to main content
Known Participant
October 26, 2010
Question

if (user is not logged in)

  • October 26, 2010
  • 1 reply
  • 762 views

Hi everyone!

If i want to show some data only to logged in users, this is the script i must use:

<?php if ($_SESSION['MM_Username']) { ?>

<div>some content for logged in users</div>

<?php } ?>

Well, i'm creating a basic cms, with a top bar for the admin user, and this works great.

Now i want to add to the cms the activate or deactivate frontend option. This option will affect all the non logged in users, and it will show only this:

<div>The site is offline, please check back later</div>

But, if the user is the admin, the frontend must keep visible

I thought in something like this:

<?php if ($_SESSION['MM_Username']) { ?>

<div>some content for logged in users</div>

<?php } ?>

<?php if (($recordset with the activate or deactivate frontend option) == 'deactivate') { // the option selected in the form is "the site is offline"

     if (user is not logged in) { ?> // What's the correct syntax to write this?

<div>Site offline, please check back later</div>

<?php

     }

} else { ?>

<div>whole site</div>

<?php } ?>

Thanks in advance to all!

Cheers

This topic has been closed for replies.

1 reply

October 26, 2010

If SESSION for username is null then the user is not logged in, thus:

<?php if ($_SESSION['MM_Username'] != '') { ?>

<div>you are logged in</div>

<?php } elseif ($_SESSION['MM_Username'] == '') { ?>

<div>you are not logged in</div>

<?php } ?>

<div>whole site</div>

javissssAuthor
Known Participant
October 26, 2010

Yeah, thanks. But one thing:

I don't Want to show one or another content. I want this:

Option: Site DeactivatedOption: Site Activated

If non logged in user:

<html>

<body>

<div>Site Deactivated</div>

</body>

</html>

If non logged in user:

<html>

<body>

<div>Whole site</div>

</body>

</html>

If logged in user (admin)

<html>

<body>

<div>Whole site</div>

</body>

</html>

If logged in user (admin)

<html>

<body>

<div>Whole site</div>

</body>

</html>

I made this script, but didn't work well:

<div class="container">

<?php if (($row_rsxx['dbdata'])=='Deactivated') {

if (empty($_SESSION['MM_Username'])) { ?>

<div>Site offline</div>

<?php }

} else { ?>

<div>WHOLE SITE</div>

<?php } ?></div>

For non logged in users this scripts works like charm. But, for the admin view, doesn't show the site. I thinks it's because of this:

The script says:

if -> option is 'deactivated' -> keep going

if -> session is empty

then show "site offline"

for non logged in users, this works because: if option is NOT 'deactivated', then show whole site, and if option IS 'deactivated', show "site offline"

But, for logged in users, the scripts fails after the second if, because session is not empty, and i don't know how to make the script keep going to the else (i know that the "else" it's part of the first "if", not the second)

Any ideas about that?

Cheers!

Message was edited by: javissss

javissssAuthor
Known Participant
October 26, 2010

Well, i found a solution: this is the script, if anyone need it

Because it's not a protected page, you need to add this code in the line 1 of the page:

<?php require_once('../Connections/YOURCONNECTIONFILE.php'); ?>

<?php

if (!isset($_SESSION)) {

  session_start();

} ?>

Then, assuming you need the same thing i was needing, you want this:

If the site it's deactivated, show a warning message for all non logged in users. But if it's the admin user, show the site.

1- First of all, you need to create a recordset to examine the db data in order to see if the site is activated or not. (lets assume the table is called "preferences" and the row is "pref_site_online" with an ENUM type and 2 options: 'Activated','Deactivated'.

2- Put this code right before the warning message:

//This checks if the row value is deactivated AND if the session is empty. If both are true, then show the warning.

<?php if (($row_YOURRECORDSETNAME['pref_site_online'])=='Deactivated' && (empty($_SESSION['MM_Username']))) { ?>

          <div>Site offline. Please check back later.</div>

//But, if one of this are false, then show the entire site

<?php } else { ?>

<div>YOUR SITE</div>

<?php } ?>

Thanks to all

Cheers!