Jump to content

Recommended Posts

hi, i followed phpacademys login and register system from part one to part twenty two, after this i created a nice web design

 

and added a php chatbox now what i want to know is

 

i have a function called protect_page() that redirects guests to a error message and i have a

 

has_access() function which checks the users rank, this is all good but if i want multiple ranks eg 1, 2, 3, but 4 not to access it how would i do this i am currently doing this

 

if (has_access($session_user_id, 4) === true) {
       echo 'you do not have the rights to view this page';
} else {
      echo 'You have the rights';
}

 

i would like something like this

 

if (has_access($session_user_id < 3) === true) {

}

 

but this just wont work.

 

another issue i have i have a function called BBCode used in an echo as

 

$text = '
[center]center text[/center]
';
echo BBCode($text);

 

it converts the BBCode to html i would like to use it in this file, which will work with my chatbox

 

<?
include 'core/init.php';


if(isset($user_data['first_name'])){
$text = $_POST['text'];

$fp = fopen("log.php", 'a');

fwrite($fp, '('.date('g:i A').') <b><a href="'. $user_data['username'] .'" target="_blank">'.$username.'</a></b>: '.stripslashes(htmlspecialchars($text)).'<br>
');
fclose($fp);
}
?>

 

the BBCode function is

function BBCode ($string) {
$search = array(
     '#\[center\](.*?)\[/center\]#si',
     '#\[list\](.*?)\[/list\]#si',
     '#\[li\](.*?)\[/li\]#si',
     '#\[b\](.*?)\[/b\]#si',
     '#\[br\](.*?)\[/br\]#si',
     '#\[i\](.*?)\[/i\]#si',
     '#\[u\](.*?)\[/u\]#si',
     '#\[img\](.*?)\[/img\]#si',
     '#\[url=http://(.*?)\](.*?)\[/url\]#si',
     '#\[size=(.*?)\](.*?)\[/size\]#si',
     '#\[font=(.*?)\](.*?)\[/font\]#si',
     '#\[color=(.*?)\](.*?)\[/color\]#si',
     '#\[code\](.*?)\[/code\]#si'
);
$replace = array(
     '<center>\\1</center>',
     '<ol>\\1</ol>',
     '<li>\\1</li>',
     '<b>\\1</b>',
     '<br>\\1</br>',
     '<i>\\1</i>',
     '<u>\\1</u>',
     '<img src="\\1">',
     '<a href="\\1">\\2</a>',
     '<font size="\\1">\\2</font>',
     '<font family="\\1">\\2</font>',
     '<font color="\\1">\\2</font>',
     '<code>\\1</code>'
);
return preg_replace($search, $replace, $string);
}

 

thanks

 

 

one last thing i need, the chatbox i have sends the entered data with the php code i sent above, to log.php, i was wonderign if there was a faster way of clearing this chatbox then going into the file and removing all the content form it?

would it be possible to add a word to reset it like [reset] with a similar method to the bbcode or something along those lines

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/264986-clearing-chatbox-adding-bbcode/
Share on other sites

Passing the user level to the has_access function should return a true or false, that is what you should check.  You cannot pass operators in the function arguments like that.  Being that we don't have access to the has_access() function, we cannot point you in the right direction.  However, for you second problem.

 

Replace your BBCode function with this snippet, and you can type [reset] and it will clear the file for you (it writes [reset] at the top of the file).

function resetLog() {
$logFilename = 'log.txt'; //filename.
file_put_contents($logFilename,'[reset]'); //open the file, overwrite the contents with the string [reset], close the file.
return true; //return true.
}

function BBCode ($string) {
//looking for the reset code
if(strpos($string,'[reset]') !== false) { //if string is found.
	resetLog(); //call the reset function.
	$string = str_replace('[reset]','',$string); //delete the string [reset] from the input.
}
$search = array(
     '#\[center\](.*?)\[/center\]#si',
     '#\[list\](.*?)\[/list\]#si',
     '#\[li\](.*?)\[/li\]#si',
     '#\[b\](.*?)\[/b\]#si',
     '#\[br\](.*?)\[/br\]#si',
     '#\[i\](.*?)\[/i\]#si',
     '#\[u\](.*?)\[/u\]#si',
     '#\[img\](.*?)\[/img\]#si',
     '#\[url=http://(.*?)\](.*?)\[/url\]#si',
     '#\[size=(.*?)\](.*?)\[/size\]#si',
     '#\[font=(.*?)\](.*?)\[/font\]#si',
     '#\[color=(.*?)\](.*?)\[/color\]#si',
     '#\[code\](.*?)\[/code\]#si'
);
$replace = array(
     '<center>\\1</center>',
     '<ol>\\1</ol>',
     '<li>\\1</li>',
     '<b>\\1</b>',
     '<br>\\1</br>',
     '<i>\\1</i>',
     '<u>\\1</u>',
     '<img src="\\1">',
     '<a href="\\1">\\2</a>',
     '<font size="\\1">\\2</font>',
     '<font family="\\1">\\2</font>',
     '<font color="\\1">\\2</font>',
     '<code>\\1</code>'
);
return preg_replace($search, $replace, $string);
}

thanks the reset workes but the bbcode doesn't here is my has_access function

 

function has_access($user_id, $type) {
$user_id	= (int)$user_id;
$type 		= (int)$type;

return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_id` = $user_id AND `type` = $type"), 0) == 1) ? true : false;
}

 

the reset works fine but the actual BBCode function i have isn't working

 

here is my post.php file

 

include 'core/init.php';


if(isset($user_data['first_name'])){
$text = $_POST['text'];

$fp = fopen("log.php", 'a');
$input = '('.date('g:i A').') <b><a href="'. $user_data['username'] .'" target="_blank">'.$username.'</a></b>: '.$text.'<br>';


fwrite($fp, ' ' . BBCode($input) . '
');
fclose($fp);
}

 

i dont understand it, [reset] is in the BBCode bit and yet it works when the bbcode doesn't also it would be helpful if you could find a useful method for making [reset] only work for user access level 1

The function works for me.

test

input: [b]Bold[/b]

[center]Center[/center]

[i]Italics[/i]
[list][*]Ordered List[/list]
[u]Underline?[/u]
[size=3]Font Size 3[/size]
[color=green]Green Text[/color]
<br />
Output: <b>Bold</b>
<center>Center</center>
<i>Italics</i>
<ol><li>Ordered List</li></ol>
<u>Underline?</u>
<font size="3">Font Size 3</font>
<font color="green">Green Text</font>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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