glob() is not searching folder - php

I think I'm doing something fundamentally wrong. Here is my code
index.php
<?php
if(isset($_POST['a'])){
$action = $_POST['a'];
} else if(isset($_GET['a'])){
$action = $_GET['a'];
} else {
$action = "home";
}
if($action == "home"){
$frontImages = glob('assets/images/frontpage/*');
include_once 'home.php';
}
?>
I var_dump($frontImages) inside of homp.php (which btw is showing just fine) but I'm getting that $frontImages is an undefined variable. Both the index.php file and home.php file is in the root folder and here is a image of the path directory:
So not sure what I'm doing wrong here.

use
<?php
if(isset($_REQUEST['a'])) {
$action = $_GET['a'];
} else {
$action = "home";
}
if($action == "home"){
while($acImg = glob('assets/images/frontpage/*')) {
$frontImages[] = $acImg;
}
include_once 'home.php';
}
?>
Now you have a Array but this works by me

Related

How I can create a PHP breadcrumb with $_GET

In my index.php, I use this method to generate my pages:
<?php
$pages = scandir("views/");
if(isset($_GET["p"]) && !empty($_GET["p"]) && in_array($_GET["p"].".php", $pages))
{
$page = $_GET["p"];
}
else
{
header("Location: ?p=home");
}
ob_start();
include "views/$page.php";
$content = ob_get_clean();
include "views/layout.php";
?>
I would like to create a breadcrumb like this:
How can I do that please? Using what I have in my index.php

elseif not working with header location

I'm trying to get userclass to equal home when the user is on either usercp.php or profile.php, but I can't seem to get the following elseif to work:
if ($_SERVER['REQUEST_URI'] === '/usercp.php') {
$userclass = "home";
} elseif ($_SERVER['REQUEST_URI'] === '/profile.php') {
$userclass = "home";
} else {
$userclass = "norm";
}
I recommend you to try: echo $_SERVER['REQUEST_URI']; to see the value of the variable.
DonĀ“t forget that REQUEST_URI returns the path relative to the server root directory. So, if You have your php file inside a directory called "test", the string returned by REQUEST_URI will be "/test/thefile.php"
Try something like this ?
if (strpos($_SERVER['REQUEST_URI'], 'usercp.php')) {
$userclass = "home";
} elseif (strpos($_SERVER['REQUEST_URI'], 'profile.php')) {
$userclass = "home";
}
Or echo your $_SERVER['REQUEST_URI'] and make comparison like Rizier123 comment.

My script doesn't work as expected

This is the button
<a href="index.php?p=contact">contact<a>
This is the php script:
<?php
$p = isset($_GET['p']);
if($p == "artist")
{
include 'artist.php';
}
if($p == "contact")
{
include 'contact.php';
}
if($p == "releases")
{
include 'releases.php';
}
if($p == "downloads")
{
include 'downloads.php';
}
else
{
include 'home.php';
}
?>
So my script should include contact.php when I hit the button, but instead of including only contact.php it includes all php files. (this happens also with the other buttons).
Right now your $p variable equals true (this is what isset returns).
Change $p = isset($_GET['p']); to $p = $_GET['p']; and you'll be good
Even better:
$p = isset($_GET['p']) ? $_GET['p'] : false;
in this case you're secured against p being null
EDIT:
There is also another issue with your code - last else statement. It is always true when $p is different than downloads. So either you change every if to else if like this:
if($p == 'artist')
{
include 'artist.php';
}
else if($p == 'contact')
{
include 'contact.php';
}
(...)
else
{
include 'home.php';
}
or change this to switch statement:
switch($p)
{
case 'artist':
include 'artist.php';
break;
(...)
default:
include 'home.php';
}
isset($_GET['p']) returns true or false, so the code comparing $p to some strings will always return true and run the code inside the if block.
Change $p = isset($_GET['p']) simply to $p = $_GET['p']
To make this (more) reusable, you should alter your script a bit. Create a simple whitelist, check it the parameter is allowed, and if yes, include the $_GET['p']'s value directly:
$allowed = false;
if (isset($_GET['p']))
{
switch $_GET['p'] {
case 'contact':
$allowed = true;
break;
case 'artist':
$allowed = true;
break;
// and so on for all your IFs
}
if ($allowed === true) {
include $_GET['p'].'.php'
}
else
{
die('illegal parameter found')
}
}

Show content depending on url

Is it possible to determine what to show depending on the URL?
I have an index file which is:
<?php include './includes/header.php'; ?>
<?php include './includes/menu.php'; ?>
<?php include './includes/content.php'; ?>
<?php include './includes/sidebar.php'; ?>
<?php include './includes/footer.php'; ?>
Note: I have different "content.php"'s
Is it possible to do something like:
If Url = url {
show only content for the url
}
and then have case system like
case: home.php
show some
etc
I know Wordpress can do it. Is it possible with PHP and MySQL and HTML?
EDIT: Instead of content.php i would want show the desired HTML code gotten from my db
Use this function to see your current page. Then use the "switch" case for proper include file:
## Get Current Page / Section
function cur_page()
{
$cur_page='';
if(isset($_SERVER['PHP_SELF']) && $_SERVER['PHP_SELF']!='')
{
$temp_var1 = explode('/', $_SERVER['PHP_SELF']);
$cur_page = $temp_var1[count($temp_var1)-1];
}
else if(isset($_SERVER['SCRIPT_NAME']) && $_SERVER['SCRIPT_NAME']!='')
{
$temp_var1 = explode('/', $_SERVER['SCRIPT_NAME']);
$cur_page = $temp_var1[count($temp_var1)-1];
}
else if(isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI']!='')
{
$temp_var1 = explode('/', $_SERVER['REQUEST_URI']);
$cur_page = $temp_var1[count($temp_var1)-1];
$temp_var2 = explode('?', $cur_page);
$cur_page = $temp_var2[0];
}
else if(isset($_SERVER['SCRIPT_FILENAME']) && $_SERVER['SCRIPT_FILENAME']!='')
{
$temp_var1 = explode('/', $_SERVER['SCRIPT_FILENAME']);
$cur_page = $temp_var1[count($temp_var1)-1];
}
return $cur_page;
}//end func.....
Querying from database.
I don't recommend MySql, and I hope you learn PDO instead, but just
for this example
function get_me_title($page) {
$query = "SELECT * FROM title WHERE title = $page";
$result = mysql_query($query);
foreach($result as $row) {
return $row[$page];
}
}
Now, you can use function .get_me_title('whatever') to query from database, and echo below
if(isset($_GET['page_id'])) {
$page = $_GET['page_id'];
switch($page) {
case "contact";
echo get_me_title('contact');
break;
case "about";
echo get_me_title('about');
break;
case "portofolio";
echo get_me_title('portofolio')
break;
default:
echo 'you are in home page';
}
}else {echo '404 ERROR! The Page you have requested does not exist';}
Instead of including content.php, you can include needed page.
For example, if You build Your urls, where, for example, $_GET['page'] will refer to needed page, then simply You can do this.
$availablePages = array('default' => 'home', 'about');
if (isset($_GET['page']) && in_array($_GET['page'], $availablePages) {
$page = $_GET['page'] . '.php';
} else {
$page = $availablePages['default'] . '.php';
}
include $page;

if user is blocked php include page, else include other?

Can someone please help me, i am trying to incorporate this piece of code:
<?php
$blocked_users = blocked_users();
while ($block = mysql_fetch_array($blocked_users)) {
if ($block['blocked'] == '1') {
include("includes/mod_profile/mod_blocked.php");
}
}
?>
to fit this piece of code as an else statement:
$profile_info_set = get_profile_info();
while ($profile = mysql_fetch_array($profile_info_set))
if (isset ($profile_id))
if ($user['account_status'] == "Active")
if ($user['account_type'] == "Escort") {
include("includes/mod_profile/mod_profile.php");
}
I have a table in my database that puts a users block status from 0 to 1. and if a user blocks someone and that user tries to access their profile then i am trying to make it so that the user goes to another page that says blocked. i am doing this through <?php include(.. ?>
At the moment i just tried putting this at the top of the page:
<?php
$blocked_users = blocked_users();
while ($block = mysql_fetch_array($blocked_users)) {
if ($block['blocked'] == '1') {
include("includes/mod_profile/mod_blocked.php");
}
}
?>
and whilst it is working and including the page mod_blocked.php its also bringing up mod_profile.php which is the default profile page and overlapping. So basically if a users not blocked they should go to mod_profile.php and if a users blocked they go to mod_blocked.php.
can someone please show me where im going wrong and how to achieve this?
Here's the entire page of code:
<?php
$page_title = "Profile";
include('includes/headerframe.php');
// GET PROFILE ID FROM URL
if (isset ($_GET['id'])) {
$profile_id = $_GET['id'];
}
?>
<?php
$blocked_users = blocked_users();
while ($block = mysql_fetch_array($blocked_users)) {
if ($block['blocked'] == '1') {
include("includes/mod_profile/mod_blocked.php");
}
}
?>
<?php
$user_info_set = get_user_info();
if (!$user = mysql_fetch_array($user_info_set)) {
include ('includes/mod_profile/mod_noprofile.php');
}
else if (!isset($profile_id)) {
include("includes/mod_profile/mod_noprofile.php");
}
$profile_info_set = get_profile_info();
while ($profile = mysql_fetch_array($profile_info_set))
if (isset ($profile_id))
if ($user['account_status'] == "Active")
if ($user['account_type'] == "Escort") {
include("includes/mod_profile/mod_profile.php");
}
else if ($block['blocked'] == '1') {
include("includes/mod_profile/mod_noprofile.php");
}
$profile_info3_set = get_profile_info3();
while ($profile = mysql_fetch_array($profile_info3_set))
if (isset ($profile_id))
if ($user['account_status'] == "Active")
if ($user['account_type'] == "Client") {
include("includes/mod_profile/mod_account.php");
}
?>
I'm not sure if I fully understand what you are up to, but when I got your question correct, you could do it the following way:
$page_title = "Profile";
include('includes/headerframe.php');
// stores the correct include file...
$toInclude = false;
// GET PROFILE ID FROM URL
if (isset ($_GET['id'])) {
$profile_id = $_GET['id'];
}
// look up for blocked user..
$blocked_users = blocked_users();
while ($block = mysql_fetch_array($blocked_users)) {
if ($block['blocked'] == '1') {
// don't do the include inside the loop
// we simply set the $toInclude
$toInclude = "includes/mod_profile/mod_blocked.php";
break;
}
}
$user_info_set = get_user_info();
// in case no include is set, and there is no profile...
if ( !$toInclude && (!$user = mysql_fetch_array($user_info_set) ||
!isset($profile_id)) ) {
// we set the $toInclude now
$toInclude = 'includes/mod_profile/mod_noprofile.php';
}
if($toInclude) {
// either blocked or no-profile
include($toInclude);
} else {
// user is not blocked, and you have a profile
// proceed with your code for normal user handling here
}
Couple options here.
Add break or die() like such
<?php
$blocked_users = blocked_users();
while ($block = mysql_fetch_array($blocked_users)) {
if ($block['blocked'] == '1') {
include("includes/mod_profile/mod_blocked.php");
break;
}
}
?>
Or, surround it in the else
<?php
$page_title = "Profile";
include ('includes/headerframe.php');
// GET PROFILE ID FROM URL
if (isset($_GET['id'])) {
$profile_id = $_GET['id'];
die();
}
$blocked_users = blocked_users();
while ($block = mysql_fetch_array($blocked_users)) {
if ($block['blocked'] == '1') {
include ("includes/mod_profile/mod_blocked.php");
} else {
$user_info_set = get_user_info();
if (!$user = mysql_fetch_array($user_info_set)) {
include ('includes/mod_profile/mod_noprofile.php');
} else if (!isset($profile_id)) {
include ("includes/mod_profile/mod_noprofile.php");
}
$profile_info_set = get_profile_info();
while ($profile = mysql_fetch_array($profile_info_set))
if (isset($profile_id))
if ($user['account_status'] == "Active")
if ($user['account_type'] == "Escort") {
include ("includes/mod_profile/mod_profile.php");
} else if ($block['blocked'] == '1') {
include ("includes/mod_profile/mod_noprofile.php");
}
$profile_info3_set = get_profile_info3();
while ($profile = mysql_fetch_array($profile_info3_set))
if (isset($profile_id))
if ($user['account_status'] == "Active")
if ($user['account_type'] == "Client") {
include ("includes/mod_profile/mod_account.php");
}
}
}
?>

Categories