Jump to content

How would i script a comment box?


wtfsmd

Recommended Posts

I got the user database figured out.  Now how would i code a comment box? It would require that the user was registered to post.

Also if its not to much to ask how would i set permissions in my database, for like Admin, Registered. so like only the Admin can post articles on the front page or what ever. ???
Link to comment
https://forums.phpfreaks.com/topic/33084-how-would-i-script-a-comment-box/
Share on other sites

for the admin, registed and unregistered thing you can have a row in your database called "user_level" or something and you could have 0 for registered users and 2 for admin.. then you can create a function that you can include on all of your pages.. you can make a function like this, this will check to make sure that the level requirment meets what you have set.. you will have to set a session variable after they log in also for their username or user id and their user_level so you can check that:
[code]
function checkUser($required)
{
// if the session id is not set, redirect to login page
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
            else{
                        return false;
            }

if($_SESSION['user_level'] >= $required){
return false;
}
                          else{
                                      return true;
                          }
}[/code]
this code will check for a session user_id variable and if its not their redirect to the login page.. then check to make sure the user level is greater of equal to the required level... then on your pages you can do this:
[code]
checkUser(0);
[/code]
that will make sure the user is registered and if they are they will be able to access that page.. if not they will be redirected to the login page.. or you can do this:
[code]
if(!checkUser(1)){
    echo "Sorry you are not permissioned to access this page";
}
[/code]
or you can make it automatically redirect in the function or do that here just put the Location thing in the if statment which says if the username is not admin display that.

hope that helps you!
also if you wanted to have a special part on the page like a button for only the admin to see that lets him add articles you could do something like this (using the function i already posted):
[code]
if(checkUser(1)){
      echo '<input type="button" name="post_article" method="post" onclick="window.location.href=\''process.php\">';
}
[/code]
and on the process page you could also check to make sure an admin is logged in.. this will just make sure that only admins actually see the post button..

you could do the same thing with the comment box but im not sure how you want it set up.. are you going to have the entire page only accessable to people who are registed or have a page with a only the comment box accessable to registered users? and would you want to show it anyways or not..

if it was on a page where they user had to be registered you could just put the checkUser(0) at the top and if theyre not logged in it will redirect them to the login page

if you wanted on a page where anyone can see it you can put the checkUser(0) at the top of the process page

if you wanted to have it on a page where the box is hidden if the person isnt registered you could do the same thing I did with the admin post article thing like this if(checkUser(0)){ include 'commentbox.php'; } but that will redirect them to login with the function i put so you might want to change the function by instead of redirecting return false then you can decide what to do if the user isnt logged in from there..

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.