Prevent guests from viewing the topics and / or forums

View previous topic View next topic Go down

Prevent guests from viewing the topics and / or forums

Post by Guest on 1/30/2015, 9:19 am


Guest
Guest


This JS will prevent guests from viewing forums, sub-forums and / or topics and posts. Depending on how you want guests to be able to view your forum in depth, placement may vary upon your choice. The alert text can of course be changed to your liking.


phpbb3

[script=Prevent Guests from Viewing Topics and Forums=Your choice]
Code:
$(function(){
if(_userdata.session_logged_in==0){
if(location.href.match(/\/t\d+-(.*)/)){
alert('You need to be logged in to be able to watch the topics. Please register or log in!');
location.href="/";
 }
$('.topiclist a[href^="/t"]').parent().each(function(){
var atext = $(this).html();
var atext2 = atext.replace(/(.*)href="(.*)"(.*)<\/a>/,'$1href="/"$3<font color="red"> --- Log in view</font></a>');
$(this).html(atext2);
})}
});


Example (Dutch / English):

[script=Prevent Guests from Viewing Topics and Forums=In the topics]
Code:
$(function(){
if(_userdata.session_logged_in==0){
if(location.href.match(/\/t\d+-(.*)/)){
alert('Je moet ingelogd zijn om berichten te kunnen bekijken. Registreer je nu op Friendcodes.nl of log in!\n\nYou need to be logged in to be able to watch the topics. Please register with Friendcodes.nl now or log in!');
location.href="/";
 }
$('.topiclist a[href^="/t"]').parent().each(function(){
var atext = $(this).html();
var atext2 = atext.replace(/(.*)href="(.*)"(.*)<\/a>/,'$1href="/"$3<font color="red"> --- Log in om dit verder te kunnen bekijken</font></a>');
$(this).html(atext2);
})}
});

Back to top Go down

Re: Prevent guests from viewing the topics and / or forums

Post by Ange Tuteur on 1/31/2015, 12:09 am

avatar
Ange Tuteur
Admin
Admin

Posts : 20
Reputation : 3
Join date : 2014-12-24

View user profile
I cleaned it up a little bit, and added some variables to make it easier to customize.

Code:
$(function(){
  var message = 'You need to be logged in to be able to view the topics. Please register or log in!',
      login = 'Log in to view',
      redirect = '/';
  
  if (!_userdata.session_logged_in) {
    if (window.location.pathname.match(/\/t\d+/)) {
      alert(message);
      window.location.href = redirect;
    }
    $('.topiclist a[href^="/t"]').parent().each(function(){ $(this).html($(this).html().replace(/(.*)href="(.*)"(.*)<\/a>/,'$1href="/"$3<font color="red"> --- '+login+'</font></a>')) });
  }
});

The redirect should work for all versions, it's just the loop which changes the link location that's phpbb3 specific, but that can be changed by editing this part : .topiclist a[href^="/t"]

_________________
miou

Back to top Go down

Re: Prevent guests from viewing the topics and / or forums

Post by Guest on 1/31/2015, 12:27 am


Guest
Guest


Right! That's Ange... He cleaned it up 'a little bit' Wink

The first part I understand, that's the variables (Rolling Eyes right Sam anyone could see that with his eyes shut). The second part, well, okay apart from the /\/t\d+/ dashes and smashes, but uhmmm.... that 3rd part is straight from another (cat?) planet to me...

I thanked you for the post, hoping that one day you can explain how on Earth I got that huge headache all of the sudden and why my eyes got burned reading back that last line Razz

Back to top Go down

Re: Prevent guests from viewing the topics and / or forums

Post by Ange Tuteur on 1/31/2015, 8:13 am

avatar
Ange Tuteur
Admin
Admin

Posts : 20
Reputation : 3
Join date : 2014-12-24

View user profile
This part ?

Code:
$('.topiclist a[href^="/t"]').parent().each(function(){ $(this).html($(this).html().replace(/(.*)href="(.*)"(.*)<\/a>/,'$1href="/"$3<font color="red"> --- '+login+'</font></a>')) });

I just removed the variables and placed what the variables did where atext2 is.
$(this).html(atext2);

I removed the dash in the regex, because it wouldn't work on topics with multiple pages, because /t\d+- turns into /t\d+p\d+-.


I figured this would be interesting to discuss...inversion ! The exclamation mark can be used to invert conditions, so they check for the opossite. ( e.g. false instead of true )

So instead of writing _userdata.session_logged_in == 0

we could write !_userdata.session_logged_in

in boolean 0 = false, and 1 = true.


Here's an example for the logged session. This conditional method can be used when you only need to run simple changes.

Code:
_userdata.session_logged_in ? alert('You're logged in !') : alert('You're logged out !');

This is the equivilent, but makes use of inversion.

Code:
if (_userdata.session_logged_in) {
  alert('You're logged in !');
}
if (!_userdata.session_logged_in) {
  alert('You're logged out !');
}

_________________
miou

Back to top Go down

Re: Prevent guests from viewing the topics and / or forums

Post by Guest on 1/31/2015, 11:45 am


Guest
Guest


That's a whole mouth full to me again. I'm pretty 'new' to JS. I never wrote any JS before, and although I can read and understand a bit of it, that complicated line in your first post here was dazzling me. JS isn't my game, yk, it's too complicated to comprehend and probably I wouldn't be able to differentiate between all the board versions and come up with such scripts. I like CSS better, or have the best of ideas most. And I love phpBB3 as well.

So, if I understood well, actually the exclamation mark is a sort of 'not' statement:

Code:
if (!_myname.is_Samantha) (alert('You don\'t know me!');} //the old method used boolean false = 0 for this
if (_myname.is_Samantha) (alert('You\'re my friend!');} //the old method used boolean true = 1 for this

If that's correct, it's the first JS I wrote ever.

So, if that's the new way of notation in scripts, why was the other method used so often until now? And is this new insight in things to keep bugs out? You're still using the old method on FM I saw before; why? And what's a regex, does that stand for regular expres​sion(I got a regdep, a regular depression from this lol)?

Back to top Go down

Re: Prevent guests from viewing the topics and / or forums

Post by Ange Tuteur on 1/31/2015, 10:18 pm

avatar
Ange Tuteur
Admin
Admin

Posts : 20
Reputation : 3
Join date : 2014-12-24

View user profile
It's a nice little trick that you can use, basically it's useful if you know the value is true or false. Your example depending on what the value of _myname.is_Samantha would work and not work. If it was a string, we'd have to use a comparative statement using the == and != operators.

== : equal to
!= : not equal to

Code:
var _myname {
  is_Samantha : 'Samantha',
  is_Ange : 'Who?'
};

if (_myname.is_Samantha == 'Samantha') alert('It's Samantha !');
if (_myname.is_Ange != 'Ange') alert('Not Ange');

Now if is_Samantha was boolean, we'd know when it was true or false, therefore we can utilize inversion.

Code:
var _myname {
  is_Samantha : true,
  is_Ange : false
};

if (_myname.is_Samantha) alert('It's Samantha !');
if (!_myname.is_Ange) alert('Not Ange');


It's more or less a simpler way of writing conditional statements, but you have to know when to use it. It can be useful for strings if you're checking if the length of the string is greater than 0. Since 0 is false, we could make a function return if no text was written. Any value higher than 0 is true. Let's say we have a text field, we could send an alert if the value was empty.
Code:
$(function() {
  $('body').append('<input type="text" id="testMe"/><input type="button" id="theClicker" value="Click me"/>');
  document.getElementById('theClicker').onclick = function() {
    var val = document.getElementById('testMe');
    if (!val.value.length) alert('Please write something... I\'m hungry..');
    else {
      alert('Mmm ..! Your message was tastey !!');
      val.value = '';
    }
  }
});

You can run the above in the sratchpad or your forum; it'll drop some stuff a the bottom of the page. You'll see the difference when you don't type anything and type something by pressing the button.


Back in the past I didn't use or know how to use inversion that well, nowadays I use it when I know the condition is going to be false. You can do the same with elements in the document. If an element doesn't exist it's considered "null", therefore we can use inversion to check if it exists or not. Such as the logout button :
Code:
if (document.getElementById('logout')) alert('You're logged in');
if (!document.getElementById('logout')) alert('You're logged out')

You could just use else in the above statement, but if you're just making a condition for logged out users, you can use inversion. Wink

LOL sorry for the whole book. Razz

_________________
miou

Back to top Go down

Re: Prevent guests from viewing the topics and / or forums

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

Permissions in this forum:
You cannot reply to topics in this forum