LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-18-2011, 07:00 PM   #1
countrydj
Member
 
Registered: Jun 2009
Location: Preston, England
Distribution: Centos 6
Posts: 127

Rep: Reputation: 1
Can't get the OR operator to work


Hi Guys..

I'm having a bit of trouble getting the OR operator to work.
This is the code that I have:

global.php:
Code:
$vars["admin link"] = "<a href='/path/to/admin.php'>Admin</a>";
$vars["admin ip"] = "xxx.xxx.xxx.xx1";
$vars["admin ip2"] = "xxx.xxx.xxx.xx2";

if((getenv(REMOTE_ADDR) != $vars["admin ip"])||(getenv(REMOTE_ADDR) != $vars["admin ip2"]))
{
$vars["admin link"] = "";
}
index.php:
Code:
require("admin/global.php"); 

if(getenv(REMOTE_ADDR) == $vars["admin ip"]){
echo $vars["admin link"];

		}
elseif
(getenv(REMOTE_ADDR) == $vars["admin ip2"]){
echo $vars["admin link"];

		}
I suspect, having played about with this code for quite a few hours, that it is:
if((getenv(REMOTE_ADDR) != $vars["admin ip"])||(getenv(REMOTE_ADDR) != $vars["admin ip2"]))that is causing the problem.
Can anybody advise me please.

Regards,

John C

Last edited by countrydj; 04-18-2011 at 07:03 PM.
 
Old 04-18-2011, 09:31 PM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
What exactly do you expect and is not happening.

The code in the included file is executed every time you call index.php and $vars["admin link"] might be set to "".

Later you want to echo $vars["admin link"], but when of the conditions in the if statement in the include file is satisfied that won't happen.

jlinkels
 
Old 04-19-2011, 02:08 AM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
try using and rather than or
 
Old 04-19-2011, 05:08 AM   #4
countrydj
Member
 
Registered: Jun 2009
Location: Preston, England
Distribution: Centos 6
Posts: 127

Original Poster
Rep: Reputation: 1
Hi Guys...
Thanks for taking the time and trouble to help me.

This is what used to happen:

I have an ADMIN link at the bottom of the index.php page, which is only visible if the correct ip number is accessing the page.
This is the code that worked fine.
global.php
Code:
$vars["admin link"] = "<a href='/path/to/admin.php'>Admin</a>";
$vars["admin ip"] = "xxx.xxx.xxx.xx1";

if(getenv(REMOTE_ADDR) != $vars["admin ip"]){$vars["admin link"] = "";}
index.php
Code:
require("admin/global.php"); 

if(getenv(REMOTE_ADDR) == $vars["admin ip"]){
echo $vars["admin link"];
Then, if a visitor came to the index.php page the ADMIN link would not show.
If I came (my ip being xxx.xxx.xxx.xx1) to the index.php page, the ADMIN link would show at the bottom of the page.

Now I want either of TWO ip numbers to activate the ADMIN link (maybe 3 in the future), hence the code in my first post.
This is so that I can ADMIN the site, and so can my client.

And now, using the code in my first post, it doesn't work for either of the ip numbers, which leads me to believe that my coding in the global.php file is wrong.

Hope this gives a better explanation.

Regards,

John C
 
Old 04-19-2011, 05:35 AM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Look at your statement:
PHP Code:
if((getenv(REMOTE_ADDR) != $vars["admin ip"])||(getenv(REMOTE_ADDR) != $vars["admin ip2"])) 
if REMOTE_ADDR matches admin ip then you will get false with the first test followed by a true with the second test. A false OR true will give you a true
if REMOTE_ADDR matches admin ip2 then you will get true with the first test followed by a false with the second test. A true OR false will give you a true
if REMOTE_ADDR matches neither admin address then you will get true with the first test followed by a true with the second test. A true OR true will give you a true

So no mater what your input is you will get a true

Now try:
PHP Code:
if((getenv(REMOTE_ADDR) != $vars["admin ip"])&&(getenv(REMOTE_ADDR) != $vars["admin ip2"])) 
if REMOTE_ADDR matches admin ip then you will get false with the first test followed by a true with the second test. A false AND true will give you a false
if REMOTE_ADDR matches admin ip2 then you will get true with the first test followed by a false with the second test. A true AND false will give you a false
if REMOTE_ADDR matches neither admin address then you will get true with the first test followed by a true with the second test. A true AND true will give you a true

So if there is a match with either admin ip you will get a false, otherwise you will get a true.
 
Old 04-19-2011, 09:26 AM   #6
countrydj
Member
 
Registered: Jun 2009
Location: Preston, England
Distribution: Centos 6
Posts: 127

Original Poster
Rep: Reputation: 1
Hi graemef..

Many thanks for taking the time and trouble to help me resolve my coding problem.

I did try && some time ago, but it didn't work.
I have now tried it again and it DOES work.

Obviously the first time there must have been an error in the index.php file.

However, my code is now:
global.php
Code:
if((getenv(REMOTE_ADDR) != $vars["admin ip"])&&(getenv(REMOTE_ADDR) != $vars["admin ip2"])&&(getenv(REMOTE_ADDR) != $vars["admin ip3"])){
$vars["admin link"] = "";
}
index.php
Code:
if((getenv(REMOTE_ADDR) == $vars["admin ip"])||(getenv(REMOTE_ADDR) == $vars["admin ip2"])||(getenv(REMOTE_ADDR) == $vars["admin ip3"])){
echo $vars["admin link"];

	}
Many thanks for your invaluable help.

John C
 
Old 04-19-2011, 09:35 AM   #7
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Glad it worked. You might want to read up on DeMorgan's Law
 
Old 04-19-2011, 09:38 AM   #8
countrydj
Member
 
Registered: Jun 2009
Location: Preston, England
Distribution: Centos 6
Posts: 127

Original Poster
Rep: Reputation: 1
Hi graemef..
You are a GEM !!!

Thanks,

John C
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] C++ Operator Overloading Within an Already Overloaded Operator mirlin510 Programming 8 04-17-2011 12:02 PM
[SOLVED] Simple problem getting a comparison operator to work atavus Programming 6 02-08-2010 02:28 PM
C++ overloaded operator 3 times faster than c function call ? cant work out why ? qwijibow Programming 6 01-07-2008 07:18 PM
I wuld like to use linux but im a cad operator so things like auto cad needs to work Juvencio Linux - General 7 06-15-2007 10:58 AM
operator= doesn't work with a template class The_Nerd Programming 1 05-22-2006 02:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration