How to streamline page access with user_groups and an array.

Started by rcane, April 13, 2022, 04:31:44 PM

Previous topic - Next topic

rcane

I've got several pages that rely on testing

if (!in array(*some number*, $user_info['groups']) & (!in_array(*some other number*, $user_info['groups'])) {
 //do things
}else {
 //don't do things


Rather than make edits to all these files (changing multiple numbers all over) I created a php with different arrays, named to make sense for the page they work with.

Within accessArray.php:


$firstPage = array(1, 10, 25);
$secondPage = array(1, 2, 10, 24);

And so on.

The idea was to include (/path/to/accessArray.php) in each of the pages.  But, I'm having trouble with the syntax.  No error, but it doesn't seem to restrict a regular user (user_group 0 fwiw).

if (!array_intersect($secondPage), $user_info['groups']) {

// no access
}


Is that an acceptable way to test multiple values against the person's $user_info['groups']?


Arantor

That's not how that works, I'm afraid.

array() means 'take whatever is in brackets and make an array out of it. So, if (!array()) means 'if this thing I just made an array is empty...' which it won't be because you just made it an array with content.

If you meant 'in_array' instead, in_array doesn't work for multiple values, because it will be trying to compare the entire item of $secondPage to each individual item in $user_info['groups'], rather than comparing all of them to all of them.

At which point we start talking about array_intersect.


if (count(array_intersect($firstPage, $user_info['groups'])) > 0) {
    // at least one item in $firstPage is also in $user_info['groups']
} else {
    // nothing in $firstPage matches anything in $user_info['groups']
}
Holder of controversial views, all of which my own.


rcane

Ok, yes.  array_intersect.  That IS what I was going for.

I had a host of errors, but it turned out I was including SSI.php twice, and it didn't like that.  Not sure why it doesn't like that. I had it included in the page that held the arrays I'm referencing as well as the page that has the "meat" of what I wanted to display.

If I take the include SSI.php out of the file that has all the authorization arrays (which makes it all work), and I try to visit it straight away in a browser (www.mydomain.com/auth_array.php) it just gives a blank screen--to which I'm not sure of the safety of that.








Arantor

Well, if you don't include SSI.php, $user_info is not defined so it'll never match.

require_once is your friend here, it will only include a file once and not just every single time you ask for it, meaning you can require_once on SSI.php and trust PHP to only load it the first time it's called for.
Holder of controversial views, all of which my own.


rcane

Quote from: Arantor on April 13, 2022, 05:59:38 PMWell, if you don't include SSI.php, $user_info is not defined so it'll never match.

require_once is your friend here, it will only include a file once and not just every single time you ask for it, meaning you can require_once on SSI.php and trust PHP to only load it the first time it's called for.

Ok, require_once. 

That seems to work without and issues. 

rcane

I've got a page displaying properly but I'm throwing errors.

PHP Warning:  include(home/mej/public_html/mydomain.com/SSI.php): failed to open stream: No such file or directory in /home/mej/public_html/mydomain.com/Sources/Load.php(2272) : eval()'d code on line 40

and


Failed opening 'home/mej/public_html/mydomain.com/SSI.php' for inclusion (include_path='.:/opt/alt/php74/usr/share/pear') in /home/mej/public_html/mydomain.com/Sources/Load.php(2272) : eval()'d code on line 40
And even though the array_intersect seems to work on the display page (which is a list of folder contents in an html table to view or delete) the delete button (which calls a delete.php) says the intersection's first parameter returns a null.  but, it's the same array used earlier.


I put a test.php in the same folder as SSI.php and ran __DIR__ and found the path is the same that I required.

going around to other folders of other pages that employ the in_array(*value*, $user_info['groups'] methodology I see huge error logs with similar errors--even though the pages are working.


I had been using the full path, "/home/......./SSI.php" all this time and I thought it was working.

Kindred

/home/ and home/

Are different. You need to use the first one... you are using the second
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

rcane

I'm not sure why it posted in the error log that way, but this is exactly how it appears in my .php file.   The error log prints it two different ways--which I can't speak to.

require("/home/mej/public_html/mydomain.com/SSI.php");

And for what it's worth I was familiar that the "/" makes a difference.  If it wasn't for this particular failure I wouldn't have noticed the other error logs scattered about that, although they are 'working' (unlike part of this one) they post errors.

I really need to go line by line again and see if there is an errant keystroke I missed in there.

rcane

Quote from: Kindred on April 13, 2022, 10:16:09 PM/home/ and home/

Are different. You need to use the first one... you are using the second


I'm still new at deciphering the error logs, but is it significant that the log refers to 'include' and the php file uses 'require'?

I know there are differences in how they proceed when errors are thrown, and I wondered if php just uses include to announce the 'genre' of the error?

Arantor

They do different things.

include (and include_once) load a file but failure to load is a mild warning.

require (and require_once) as the name implies means *get me that file*, it is required, and failure to load means a fatal error.
Holder of controversial views, all of which my own.


rcane

When does include and require wear off, so to speak?

Would it simply be any time you are loading a new php page and that information is needed?

As I mentioned, there are other instances where include didn't throw a fatal error, but the error was the same (can't find) yet it worked? 

It's. It clear how it can't find but can still accomplish things.

There must be an aspect of it that I can it grasp.

If this needs to go down to coding...?

Arantor

End of the page.

It's all about being loaded into the current page load. include/require "load the file regardless", include_once/require_once "load the file if this page hasn't already loaded them"

And by "the current page" I mean "whatever PHP is running to serve the request made by the browser".
Holder of controversial views, all of which my own.


rcane

As I mentioned up above, I've noticed the includes all over (whether _once or not) are working but throwing errors too.

Doing some reading I'm seeing this part of the error:

(include_path='.:/opt/alt/php74/usr/share/pear')
With the : meaning it's searching that location (/opt/alt/php..... ) as well as the path to the SSI.php I entered. One path works (as my php files are working); the other is clogging up the error_logs.


1. why does it want to ben look at that path (which I cannot find in the cPanel file manager); and
2. can I change that so it stops?

I do not see anything in the 'myDomain.com/php.ini' that references that. 

 

Arantor

Quote from: rcane on April 15, 2022, 01:03:40 PMWith the : meaning it's searching that location (/opt/alt/php..... ) as well as the path to the SSI.php I entered. One path works (as my php files are working); the other is clogging up the error_logs.

So what this means is you gave it a path to include and it didn't find it. Usually this means your path is wrong.

Whenever you do an include, it will try to resolve the path relative to where you are - this is not obvious because 'where you are' isn't a set place. If you visit example.com/include.php and include.php has include('myfile.php') it will assume it should look in the current directory.

But if you have example.com/myfolder/mypage.php that then includes include.php, where is 'current directory'? Because it's now not the place where include.php is - the 'current working directory' is now the myfolder folder, and not the root folder.

This is why it's generally good practice to use full paths in things, starting from / so PHP doesn't try to guess. What it's saying here is 'I looked in . (the current directory), and I looked in the folder I know normally to try and nothing'.

There's a reason SMF has require_once($sourcedir . '/Whatever.php'); everywhere - it's so $sourcedir is an absolute path.

If you must use relative paths, use __DIR__ to indicate 'when you find __DIR__ use the directory *this file is in* not where you are currently working'.

Quote from: rcane on April 15, 2022, 01:03:40 PM1. why does it want to ben look at that path (which I cannot find in the cPanel file manager); and

Because that's the path your host has defined in the master php.ini in the include_path directive.

Specifically, .:/opt/alt/php74/usr/share/pear means two places (with the : as a separator between the two). The . is 'the current directory' which is the folder in which your PHP file lives, the one that is actually requested by the user. The other is for a shared library of resources known as PEAR that some applications (old ones; PEAR was effectively deprecated a decade ago) still rely on.

If you don't specify an include_path in your custom php.ini, the system default will be used instead.

The hint here is DON'T CHANGE THIS, IT'S NOT BROKEN.

Quote from: rcane on April 15, 2022, 01:03:40 PM2. can I change that so it stops?

Yes, make your script work correctly by using the correct paths. The whole issue is that you haven't given PHP enough to work from and it's saying 'uh, I can't find what you're asking for, here's where I tried'.
Holder of controversial views, all of which my own.


rcane

Ok, makes sense.

I've made a "where.php" with echo(__DIR__) in it and placed it where SSI.php is as well as where my index.php is.  I have the absolute paths to both.

/home/username/public_html/mydomain.com/ is what comes up for SSI.php as that is where SMF is installed.

/home/username/public_html/mydomain.com/resources/index.php is the index file's residence.

I wrote:   include_once('/home/username/public_html/mydomain.com/SSI.php');

I even went with:   include($_SERVER['DOCUMENT_ROOT']."/SSI.php");


both make the page work--from within SMF I'm able to bring up the page and present the 'goods'.

The error log continues to complain about those other paths. 

Which php.ini is being looked at here?  I never asked that.  the one from my domain's root (domain.com/php.ini) where SMF is installed only has about 8 lines in it.


Arantor

I'd never, ever suggest using $_SERVER['DOCUMENT_ROOT'] because it might not be what you think it is, so absolute paths are the way to go.

Here's the thing, you've presented half the error. What's the *full* error?

As for which php.ini is being looked at, your tiny one followed by backfilling the rest of the (many) configuration items from the system one, followed by backfilling from PHP's own defaults.
Holder of controversial views, all of which my own.


rcane

Quote from: Arantor on April 15, 2022, 03:33:00 PMI'd never, ever suggest using $_SERVER['DOCUMENT_ROOT'] because it might not be what you think it is, so absolute paths are the way to go.

Here's the thing, you've presented half the error. What's the *full* error?

As for which php.ini is being looked at, your tiny one followed by backfilling the rest of the (many) configuration items from the system one, followed by backfilling from PHP's own defaults.

Here's the full error log, for accessing the page one time.  I deleted the error log prior to running so this is all of it.

[15-Apr-2022 15:01:47 America/Chicago] PHP Warning:  include(home/mej/public_html/mydomain.com/SSI.php): failed to open stream: No such file or directory in /home/mej/public_html/mydomain.com/Sources/Load.php(2272) : eval()'d code on line 40

[15-Apr-2022 15:01:47 America/Chicago] PHP Warning:  include(home/mej/public_html/mydomain.com/SSI.php): failed to open stream: No such file or directory in /home/mej/public_html/mydomain.com/Sources/Load.php(2272) : eval()'d code on line 40

[15-Apr-2022 15:01:47 America/Chicago] PHP Warning:  include(): Failed opening 'home/mej/public_html/mydomain.com/SSI.php' for inclusion (include_path='.:/opt/alt/php74/usr/share/pear') in /home/mej/public_html/mydomain.com/Sources/Load.php(2272) : eval()'d code on line 40

[15-Apr-2022 15:01:47 America/Chicago] PHP Notice:  Undefined index: href in /home/mej/public_html/mydomain.com/Sources/Load.php(2272) : eval()'d code on line 549


Arantor

Well, you don't have the leading / on the include() statement, meaning that it won't be treating it as an absolute path but relative.

I don't know where the code is running, because it's being caught by the eval() in Load.php, which suggests it's in a template somewhere or possibly a PHP block. But either way you have an include() statement somewhere with the wrong path in it.
Holder of controversial views, all of which my own.


rcane

Quote from: Arantor on April 15, 2022, 04:14:41 PMWell, you don't have the leading / on the include() statement, meaning that it won't be treating it as an absolute path but relative.

I don't know where the code is running, because it's being caught by the eval() in Load.php, which suggests it's in a template somewhere or possibly a PHP block. But either way you have an include() statement somewhere with the wrong path in it.

Back in post 7 i mentioned that.  The error log is leaving off the /.  This is copied directly from the php file.:


('/home/mej/public_html/mydomain.com/SSI.php'); //LOAD SSI

It doesn't matter if I put () around it or encapsulate in ''.  The log drops the "/".

The code is running in a folder, two levels down from mydomain.com.



I checked with the host and they said it was coding--not their end. 

Arantor

No, the error log isn't "leaving anything off". It's reporting *exactly* what you gave it.

Somewhere in the code - not this custom page - you have an include that is missing the leading /.

The code is being called from a PHP block in a portal, or one of SMF's templates.
Holder of controversial views, all of which my own.


Advertisement: