You must log in to view this

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ankhmor
Forum Newbie
Posts: 12
Joined: Fri Jul 13, 2007 4:45 pm

You must log in to view this

Post by ankhmor »

I'm about to start building a site.
I want to have a function on my site that you need to log in to view text that is enclosed in [hide][/hide].

How do I do that?

Or maybe I just enclose the text in [hide][hide] and then use explode?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Code: Select all

if($logged_in)
{
    //show text
} else
{
     echo 'you need to login';
}
:?:
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: You must log in to view this

Post by califdon »

ankhmor wrote:I'm about to start building a site.
I want to have a function on my site that you need to log in to view text that is enclosed in [hide][/hide].

How do I do that?

Or maybe I just enclose the text in [hide][hide] and then use explode?
I can't make any sense out of your question. It sounds like you're asking how to authenticate a web page; if so, that's a question that requires a much longer answer than can be given in a forum.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

You can filter out the hidden data for users not logged in. You could also replace it with something else..

Code: Select all

if (!$is_logged_in)
{
    $display_data = preg_replace('#\[hide\].+?\[hide\]#im', '', $display_data);
}
Or..

Code: Select all

if (!$is_logged_in)
{
    $display_data = preg_replace('#\[hide\].+?\[hide\]#im', '<strong>You must logged in to view this content</strong>', $display_data);
}
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

If the user isn't logged in, preg_replace it out.

Code: Select all

echo preg_replace('#\[hide\](.*?)\[/hide\]#', '', $content);
ankhmor
Forum Newbie
Posts: 12
Joined: Fri Jul 13, 2007 4:45 pm

Post by ankhmor »

Oh. Thanks the preg_replace function is what I'm looking for. And to think of it I use preg_split and preg_match in everyday of my life.
Post Reply