Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if statement help 2

Status
Not open for further replies.

calv1n

Programmer
Sep 29, 2007
18
CA
I have a problem with this code I'm using:

Code:
                //if (!empty($_COOKIE['$cookiename'])) {
                if ($isprivate != "1") {
                	$message = nl2br($message);
                	echo "$message";
                } else {
                	echo "<i>This message has been marked private. You must log in as an administrator to view it.</i>";
                }

Basically, I want the script to display the private message if the administrator is logged in. If not logged in, then display the message This message has been marked private. You must log in as an administrator to view it.. $isprivate is the variable to see if the message is private. i.e 1=yes, 0=no

I've been trying to make this work for a few days already, and I just can't get it.

Any help will be greatly appreciated.

Thanks.

-CALV1N
 
if 1 means that the message is private, why are you testing to see whether isPrivate is NOT 1? i think you may have the conditional inverted.

Code:
if ($isprivate == "1") { //or if you are certain of the string type use ===
  echo nl2br($message);
} else {
  echo "<i>This message has been marked private. You must log in as an administrator to view it.</i>";
}
 
Personnally, I prefer to avoid 0 and 1 in order to avoid confusion.

I'd rather do :

Code:
if ($is_logged != "yes") { ...
 
Yeah, but I don't know how to add if (!empty($_COOKIE['$cookiename'])) { to see if the admin is logged in to display the message.

When a user posts, there is an option if he/she wants the message to be private, requiring an admin to be logged in.

Like I said 1=yes and 0=no. That is the variable of $isprivate. So in short, if isprivate = 1, check if admin logged in, then display the message; or if not logged in, display the error message. And if isprivate = 0, publicly display the message.

I honestly have no idea how to do this. I am relatively new at PHP coding.

Thank you.



-CALV1N
 
Code:
if (!empty($_COOKIE['$cookiename']))

this does not work because you are using singlequotes. variables are not expanded within single quotes.

try this instead

Code:
if (!empty($_COOKIE[$cookiename]))
 
but actually the conditionality is the wrong way round anyway.

Code:
if ($isprivate == "1") { //or if you are certain of the string type use ===
  if (isAdmin()) {
    echo nl2br($message);
  } else {
    echo reject();
} else {
  echo reject();
}
function reject(){
 echo "<i>This message has been marked private. You must log in as an administrator to view it.</i>";
}

function isAdmin(){
 global $cookiename;
 return (!empty($_COOKIE[$cookiename]));
}
 
okay, so I kind of tweaked the code you've provided, and it still doesn't display the message, even though I am logged in:

Code:
if ($isprivate == "1") {
	if (!empty($_COOKIE[admin])) {
		echo nl2br($message);
	} else {
		echo "<i>This message has been marked private. You must log in as an administrator to view it.</i>";
	}
} else {
	echo nl2br($message);
}

It looks right. What's wrong? :-(

-CALV1N
 
And I don't know if this is much help, but viewing the cookies in Firefox I see this:

Name: admin
Value: CALV1N
Host: Localhost
Path: /calvin/cp/

Thanks.

-CALV1N
 
lol

I had to change the cookie path! DUH!!!

Thanks, jpadie for all your help!!

-CALV1N
 
if you are not using a variable to reference an array element, and the key is associative, you need to enquote it.

so this is wrong
Code:
 if (!empty($_COOKIE[admin])) {

and this is right
Code:
 if (!empty($_COOKIE[[red]'[/red]admin[red]'[/red]])) {

the only time you do not need to enquote an array element (if it is associative) is inside double quotes
Code:
echo "the value of the cookie variable is $_COOKIE[admin]";

in all other cases php will throw a notice.
 
Why not do it the other way around?
eg.

Call a function getMessage()
This function checks if the user is logged in, if he/she has the permissions and then returns the result.

It would be a much more reusable function and it would be much cleaner, if designed properly.

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top