scandir for files for users - php

I've created a pages dashboard for my content management system, and well I am trying to iterate the scandir method and my conditions are not working. Can someone explain why?
$pages = scandir('../',0);
foreach($pages as $page){
if($page !== "." OR $page !== ".." OR $page !== "admin" OR $page !== "admin.php" OR $page !== "kms_kontent" OR !is_dir('../'.$page)){
$extension = explode('.',$page);
$extension = end($extension);
$page = str_replace('.'.$extension,'',$page);
echo '<div class="row"><ul><li class="fifths">'.$page.'</li><li class="fifths"></li></ul></div>';
}
}
It's echoing all contents
--public_html
-admin.php #don't show
-admin #don't show
-kms_content #don'tshow
-index.php #show
-shop.php #show
Also it's showing . and .. which I don't want to show either.

The logic is flawed. To see why, let's assume you have just this comparison:
$page !== "." OR $page !== ".."
The condition is satisfied if page isn't . or if page isn't ..; if the page is . it will not be equal to .. and vice versa. In other words, the condition is always satisfied.
What you want is that the condition is satisfied only if page is neither . nor ..; so:
!($page === '.' || $page === '..')
Following DeMorgan's law:
$page !== '.' && $page !== '..'
An array of possible values could be used to simplify the code even more:
$invalid_pages = [".", "..", "admin", "admin.php", "kms_kontent"];
if (!in_array($page, $invalid_pages) && !is_dir('../'.$page)) {
}

if (
$page !== "." OR
$page !== ".." OR
$page !== "admin" OR
$page !== "admin.php" OR
$page !== "kms_kontent" OR
!is_dir('../'.$page)
) {
Because you are using OR, the if will go true when any of the conditions is true. So if $page is admin it will go false for $page !== "admin" but true to all others, and it won't work. You should use && or AND.
But instead, you could clean up that code a little bit using arrays. For instance:
$filter = ['.', '..', 'admin', 'admin.php', 'kms_kontent'];
$pages = scandir('../', 0);
foreach ($pages as $page) {
if (!in_array($page, $filter) && !is_dir("../$page")) {
...
}
}
It does the same thing, but the code looks cleaner and more items can be added more easily.

Using AND or && instead of OR!

Related

PHP check root of url

I am using below script to create sessions
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (
false !== strpos($url,'home') ||
false !== strpos($url,'display-job') ||
false !== strpos($url,'search-results-jobs') ||
false !== strpos($url,'find-jobs') ||
false !== strpos($url,'edit-profile1') ||
false !== strpos($url,'my-account/?myacount=1')
)
{
$_SESSION['page_name'] = 'jobseeker';
}
else {
$_SESSION['page_name'] = 'employer';
}
I can use the above script to check if someone is on one of the following sub pages but the problem is that i want to trigger a different session when someone is on the root of the webpage and I cant figure a way out.
try this
$url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$root_url = $_SERVER['SERVER_NAME'];
$arr_url = explode("/", $url);
unset($arr_url[sizeof($arr_url)-1]);
$new_url = implode("/",$arr_url);
if($new_url==$root_url)
{
// do your work
}

PHP if condition with and and or

I have this condition here:
if($_SERVER['REQUEST_URI'] != '/page.php' || ($_SERVER['REQUEST_URI'] != '/' && $_SERVER['REQUEST_URI'] != '/index.php')){
//do something, but not on index or page . php
}
it works on the index page, but not page.php...what am i doing wrong?
You can do it easier...
if( ! in_array( $_SERVER['REQUEST_URI'], array("/page.php", "/", "/index.php") ) ) {
// do something...
}
You have logical error use
if(!($_SERVER['REQUEST_URI'] == '/page.php' || $_SERVER['REQUEST_URI'] == '/' || $_SERVER['REQUEST_URI'] == '/index.php')){
//do something, but not on index or page . php
}
or better
if(!in_array($_SERVER['REQUEST_URI'], ["/page.php", "/", "/index.php"])){
//do something, but not on index or page . php
}

Idea with php logic using operators

I am new to PHP and find it very hard to explain.
I have a PHP navigation script with 5 categories and two variables relevant to my question:
$catname = 'used-cars'; // the same value for all categories
$currentpage; // pages 1 to 5
index.php of my site has $currentpage == '1'
The issue is that I need a logic that will say:
If $catname IS NOT 'used-cars', do something, BUT, IF $currentpage is equal to 1, even if $catname is 'used-cats' do it anyway
I am thinking of something like this:
if($catname != 'used-cars' && !($currentpage > '1')):
endif;
Hope you can help!
This is merely a single or condition. On the right side, $currentpage === 1 will evaluate to TRUE without regard to the value of $catname. If either part of the condition is TRUE, you'll enter the if () block to execute your code there.
if ($catname !== "used-cars" || $currentpage === 1) {
// do your thing
}
This is just:
if (strcmp($catname, 'used-cars') != 0 || $currentpage == 1)
(Careful with the string comparison.)
Alternatively, you could declare it as a boolean first:
$proceed = false;
if($catname != 'used-cars')
$proceed = true;
if($currentpage == 1)
$proceed = true;
if($proceed){
// whatever you want
}
$doflag = 0;
if($catname != 'used-cars')
{
$doflag = 1;
} else if($currentpage == 1) {
$doflag = 1;
}
if($doflag == 1) {
//do something
}
Basically instead of trying to do everything with the block, use the block to set a flag and use the flag to do something.

?page=index $_GET

For this moment using this code:
if ($_GET['page'] == 'index'
and file_exists('./intl/tpl/' . $_GET['page'] . '.tpl')
or !file_exists('./intl/tpl/' . $_GET['page'] . '.tpl')
or !$_GET['page']) {
//code
} elseif ($_GET['page'] == 'multi'
and file_exists('./intl/tpl/' . $_GET['page'] . '.tpl')) {
//code 2
}
and so on...
Question 1: Does this code "good" ? Doesn't need any escaping or something ?
Question 2: ?page=logout doens't work, so i created logout.php which looks like:
<?php
require_once "./intl/config.php";
SessionDelete('logged_in');
SessionDelete('username');
SessionDelete('userid');
if ($user_admin != null) {
SessionDelete('inadmin');
if (SessionGet('s_order') != null or SessionGet('s_page_show_all') != null) {
SessionDelete('s_order');
SessionDelete('s_page_show_all');
}
}
header('Location: '.$config['indexurl'].'index.php');
?>
Maybe before sessions delete need session start and it's possible do that with ?page=logout ?
The code certainly can be improved:
Reorder the tests so that it will not generate E_NOTICE errors
Parenthesize so that operator precedence is immediately obvious
Use the && and || boolean operators (as garvey's comment says)
Doing this, you 'd have:
if (empty($_GET['page']) ||
!file_exists('./intl/tpl/' . $_GET['page'] . '.tpl') ||
($_GET['page'] == 'index' && file_exists('./intl/tpl/' . $_GET['page'] . '.tpl')) {
//code
}
} elseif ($_GET['page'] == 'multi' && file_exists('./intl/tpl/' . $_GET['page'] . '.tpl')) {
//code 2
}
Then, rewrite it some more to make it clear why you are doing what you do. This will also allow you to write simpler code. Simple is good.
// This way it's obvious that 'index' is the default page
$page = !empty($_GET['page']) ? $_GET['page'] : 'index';
if (!file_exists('./intl/tpl/' . $page . '.tpl')) {
$page = 'index'; // comment here saying that if a non-existing page is requested, we display the index instead
}
$template = './intl/tpl/' . $page . '.tpl';
// At this point, we know that $page has a value, and we know that $template exists, so:
switch($page) {
case 'index':
// code
break;
case 'multi':
// code2
break;
}
As for the second question: yes, you need to start the session before you are able to modify or destroy it.

php $_GET['page'] several pages

if (!$_GET['page'] || preg_match('/\W/', $_GET['page']) || !file_exists('./intl/tpl/tpl_source/' . $_GET['page'] . '.tpl'))
$_GET['page'] = 'index';
if ($_GET['page'] && $_GET['page'] != 'index') {
$smarty->assign("pg_" . $_GET['page'], true);
$smarty->display($_GET['page'] . ".tpl");
die();
}
This code let me open any page (?page=1, ?page=2 and so on, also it's mean if no page give, open index)
but i need specify which one user can open, so, code should look like:
if ($_GET['page'] = '21' || preg_match('/\W/', $_GET['page']) || file_exists('./intl/tpl/tpl_source/' . $_GET['page'] . '.tpl')) {
//my stuff
}
In short, i need specify which addresses user can open with $_GET['page'] (?page=21 ?page=22 and so on).
Sorry if question not clear.
You can simplify your code by using a typecast (for filtering!) and a simpler list of allowed pages:
$allowed_pages = array(1, 12, 21, 25, 32);
$page = (int)$_GET["page"]
and in_array($page, $allowed_pages)
and file_exists("./intl/tpl/tpl_source/$page.tpl")
or $page = "index";
$smarty->assign("pg_$page", true);
$smarty->display("$page.tpl");
die();
You can create a white list:
var $pages = array(
21 => true,
22 => true
);
// or
var $pages = array_flip(array(21, 22));
and test whether the page is in there:
if(isset($pages[$_GET['page']])) {
}

Categories