Correctly source frontpage in PHP function - php

I've been making a header.php file for my website and I want the title tag of the page to change based on which page you're visiting.
It seems to work for everything but the front page!
Here's the function I wrote:
<?php
$directoryURI = $_SERVER['REQUEST_URI'];
$path = parse_url($directoryURI, PHP_URL_PATH);
$components = explode('/', $path);
$first_part = $components[1];
$title = "";
$jumbotitle = "";
$url = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if(strpos($url, '/') !== false){
$title = "Home";
$jumbotitle = "Home";
}if(strpos($url, 'about') !== false){
$title = "About";
$jumbotitle = "About";
}
if(strpos($url, 'contact') !== false){
$title = "Contact";
$jumbotitle = "Contact";
}
?>
Any suggestions?

Firstly $_SERVER[REQUEST_URI] should be enough
$url = $_SERVER[REQUEST_URI];
Please show us, what is $url value now ?

Related

PHP script fails to get correct home page

This script is a function to define site url to send it to the big script but because it is shared hosting it gets incorrect path how can tell it the correct site path ? I wanna tell it the site url and script dir directly the script thinks this is the path /hermes/bosnaweb23a/index.php but the correct path is /index.php only.
I wanna remove /hermes/bosnaweb23a/
because /hermes/bosnaweb23a/ is the shared hosting path
<?php
function home_base_url(){
$base_url = (isset($_SERVER['HTTPS']) &&
$_SERVER['HTTPS']!='off') ? 'https://' : 'http://';
$tmpURL = dirname(__FILE__);
$tmpURL = str_replace(chr(92),'/',$tmpURL);
$tmpURL = str_replace($_SERVER['DOCUMENT_ROOT'],'',$tmpURL);
$tmpURL = ltrim($tmpURL,'/');
$tmpURL = rtrim($tmpURL, '/');
if (strpos($tmpURL,'/')){
$tmpURL = explode('/',$tmpURL);
$tmpURL1 = $tmpURL[0];
$tmpURL2 = $tmpURL[1];
$tmpURL = $tmpURL1;
if(!empty($tmpURL2)) $tmpURL .= '/'.$tmpURL2;
}
if ($tmpURL !== $_SERVER['HTTP_HOST'])
$base_url .= $_SERVER['HTTP_HOST'].'/'.$tmpURL.'/';
else
$base_url .= $tmpURL.'/';
$base_url = str_replace('//','/',$base_url);
$base_url = str_replace('http:/','http://',$base_url);
$base_url = str_replace('https:/','https://',$base_url);
return str_replace(dirname(__FILE__),'',$base_url);
}
$local_path = dirname(__FILE__).'/';
$sSoftware = strtolower( $_SERVER["SERVER_SOFTWARE"] );
function getSlashes() {
$sSoftware = strtolower( $_SERVER["SERVER_SOFTWARE"] );
if ( strpos($sSoftware, "microsoft-iis") !== false ) return "\\";
else return "/";
}
if ( strpos($sSoftware, "microsoft-iis") !== false ) {
$local_path = str_replace(getSlashes(), '/', dirname(__FILE__)).'/';
}
function get_domain() {
return $_SERVER['HTTP_HOST'];
}
$remote_path = home_base_url();
if((strpos($remote_path, '127.0.0.1') !== false) || (strpos($remote_path, 'localhost') !== false)) {
$find = str_replace(' ','',":\ ");
#$local_path = end(explode($find,$local_path));
define('DOCUMENT_ROOT', '/'.$local_path);
}else{
define('DOCUMENT_ROOT', '/'.$local_path);
}
?>
You could get index.php from /hermes/bosnaweb23a/index.php by using this code.
$url = explode("/", "/hermes/bosnaweb23a/index.php");
$script_name = end($url);
$script_name would be index.php.

PHP script to dynamically generate links to the directory, subdirector and files present in current diectory

Below is the php script I found to dynamically generate links to files present in current directory. can anyone of u help me with php script to dynamically generate links to all the directory ,sub-directory and files present in the root directory.
<?php
$dir_open = opendir('.');
while(false !== ($filename = readdir($dir_open))){
if($filename != "." && $filename != ".."){
$link = "<a href='./$filename'> $filename </a><br />";
echo $link;
}
}
closedir($dir_open);
?>
use scandir to get all the files and all the directories in one directory, then use is_dir to make them apart.
<?php
$fileAndDir = scandir('/home/me');
if($fileAndDir !== FALSE)
{
foreach($fileAndDir as $v)
{
if(is_dir($v))
$dirs[] = $v;
else
$files[] = $v;
}
}
print_r($files);
print_r($dirs);
For all subdirs refer to this post
Try this:
** UPDATE ** To show file name instead of the full path
<?php
$directory = new \RecursiveDirectoryIterator($path);
$iterator = new \RecursiveIteratorIterator($directory);
$files = array();
foreach ($iterator as $info) {
echo '' . $info->getFilename() . '<br />';
}
you can do this by having index.php for each directory .. all having this code:
INDEX.PHP
<?php
$currentDir = dirname(__FILE__);
$subDir = scandir($currentDir);
$linkList = '<ul>';
foreach($subDir as $name)
{
$link = '<li>';
$myDirLink = '' . $name . '';
$myFileLink = '' . $name . '';
$link .= is_dir($name) ? $myDirLink : $myFileLink;
$link .= '</li>';
$linkList .= $link;
}
$linkList .= '</ul>';
echo $linkList;
?>

How to make directory within directory by php loop?

How to make directory within directory by php loop?
Example: http://site_name/a/b/c/d
First create a then b within a then c within b then ....
Problem is here a,b,c,d all the folders created in root directory not one within one.
Here is my code -
<?php
$url = "http://site_name/a/b/c/d";
$details1 = parse_url(dirname($url));
$base_url = $details1['scheme'] . "//" . $details1['host'] . "/";
if ($details1['host'] == 'localhost') {
$path_init = 2;
}else {
$path_init = 1;
}
$paths = explode("/", $details1['path']);
for ($i = $path_init; $i < count($paths); $i++) {
$new_dir = '';
$base_url = $base_url . $paths[$i] . "/";
$new_dir = $base_url;
if (FALSE === ($new_dir = folder_exist($paths[$i]))) {
umask(0777);
mkdir($new_dir . $paths[$i], 0777, TRUE);
}
}
function folder_exist($folder)
{
// Get canonicalized absolute pathname
$path = realpath($folder);
// If it exist, check if it's a directory
return ($path !== false AND is_dir($path)) ? $path : false;
}
?>
please check this code. it will create nested folder if not exit
<?php
$your_path = "Bashar/abc/def/ghi/dfsdfds/get_dir.php";
$array_folder = explode('/', $your_path);
$mkyourfolder = "";
foreach ($array_folder as $folder) {
$mkyourfolder = $mkyourfolder . $folder . "/";
if (!is_dir($mkyourfolder)) {
mkdir($mkyourfolder, 0777);
}
}
hope it will help you
You can actually create nested folders with the mkdir PHP function
mkdir($path, 0777, true); // the true value here = recursively
Dear friends the following answer is tested and used in my script -
<?php
$url = "http://localhost/Bashar/abc/def/ghi/dfsdfds/get_dir.php";
$details = parse_url(dirname($url));
//$base_url = $details['scheme'] . "//" . $details['host'] . "/";
$paths = explode("/", $details['path']);
$full_dir = '';
$init = ($details['host'] == 'localhost') ? '2' : '1';
for ($i = $init; $i < count($paths); $i++) {
$full_dir = $full_dir . $paths[$i] . "/";
if (!is_dir($full_dir)) {
mkdir($full_dir, 0777);
}
}
?>

echo all pages with each pages unique title from directory/folder

I have a folder in the main root, News. In that folder, I have 10 pages, each one has a unique page title:
/news/Today-is-cold-outside.php title = Today is Cold
/news/Watch-out-for-the-smelly-frogs.php title = Smelly Toads
I can get the code below to work so that it will fetch each page and provide a link to each page with the page name $file, example: Today-is-cold-outside.php, not $title = Today is Cold.
How can I get the page to display the specific Page Title for each link $title?
I know I have $file listed below instead of $title in the link, I left it in there so you can see where I am wanting to display the title and that if you test it, it will show you that it is getting the $file=pages from the directory/folder news.
Thanks in advance, I have been working for 2 days on this and cant find a solution.
<?php
$handle = opendir('news');
$dom = new DOMDocument();
while (false !== ($file = readdir($handle))){
$extension = strtolower(substr(strrchr($file, '.'), 1));
if ($extension == 'html' || $extension == 'htm' || $extension == 'php') {
if($dom->loadHTMLFile($urlpage)) {
$list = $dom->getElementsByTagName("title");
if ($list->length > 0) {
$title = $list->item(0)->textContent;
}
}
echo "<a href='news/$file'>$file</a>";
}
}
?>
Try nodeValue
$title = $list->item(0)->nodeValue
change
$dom->loadHTMLFile($file)
and
echo "<a href='blog/$file'>$title</a>";
If $file = Today-is-cold-outside.php
And you want $title to = Today is Cold
Do this:
$title = $file;
$title = str_replace('-',' ',$title);
$title = str_replace('.php','',$title);
echo $title;
This will convert dashes to spaces, and remove the .php extension.
If you like my answer I can also convert each word from lowercase into uppercase as well as limit the max number of words to 3 if you want.
Try this out:
<?php
$handle = opendir('news');
$dom = new DOMDocument();
while (false !== ($file = readdir($handle))){
$extension = strtolower(substr(strrchr($file, '.'), 1));
if ($extension == 'html' || $extension == 'htm' || $extension == 'php') {
if($dom->loadHTMLFile($urlpage)) {
$list = $dom->getElementsByTagName("title");
if ($list->length > 0) {
$title = $list->item(0)->textContent;
}
}
$title = $file;
$title = str_replace('-',' ',$title);
$title = str_replace('.php','',$title);
echo '<a href="blog/'.$file.'">';
echo $title;
echo '</a>';
}
}
?>
<?php
$handle = opendir('blog');
$dom = new DOMDocument();
while (false !== ($file = readdir($handle))){
$extension = strtolower(substr(strrchr($file, '.'), 1));
if ($extension == 'html' || $extension == 'htm' || $extension == 'php') {
if($dom->loadHTMLFile('blog/'.$file)) {
$list = $dom->getElementsByTagName("title");
if ($list->length > 0) {
$title = $list->item(0)->textContent;
}
}
echo "<a href='blog/$file'>$title</a><br/>";
}
}
?>
<?php
$title = $file;
$title = str_replace('-',' ',$title);
$title = str_replace('.php','',$title);
echo '<a href="blog/'.$file.'">';
echo $title;
echo '</a>'; ?>
<?php
$handle = opendir('news');
$dom = new DOMDocument();
$title = '';
while (false !== ($file = readdir($handle))){
$extension = strtolower(substr(strrchr($file, '.'), 1));
if ($extension == 'html' || $extension == 'htm' || $extension == 'php') {
if($dom->loadHTMLFile('news/'.$file)) {
$list = $dom->getElementsByTagName("title");
if ($list->length > 0) {
$title = $list->item(0)->textContent;
}
}
echo "<a href='news/$file'>$file</a>";
echo $title;
}
}
?>
my directory name is "news"
<?php
$handle = opendir('news');
$dom = new DOMDocument();
while (false !== ($file = readdir($handle))){
$extension = strtolower(substr(strrchr($file, '.'), 1));
if ($extension == 'html' || $extension == 'htm' || $extension == 'php') {
if($dom->loadHTMLFile('news/'.$file)) {
$list = $dom->getElementsByTagName("title");
if ($list->length > 0) {
$title = $list->item(0)->textContent;
}
}
echo "<a href='news/$file'>$title</a><br/>";
}
}
?>

Escaping forward slashes in an strpos() declaration

I have the following code to set a variable, $lang, depending on what directory we are currently in:
$find_lang = $_SERVER['REQUEST_URI'];
if (strpos('$find_lang', '/fr/') !== false) {
$lang = "fr";
}
else if (strpos('$find_lang', '/de/') !== false) {
$lang = "de";
}
else {
$lang = "en";
}
echo $lang;
parse_ini_file($lang . ".ini");
However, echoing $lang always gives me "en", even if I'm in http://example.com/fr/. I believe it's because of the forward slashes around the directory, but I've tried escaping as follows: //fr// and \/fr\/ but neither gives me a different answer.
Any ideas?
$find_lang = $_SERVER['REQUEST_URI'];
if (strpos($find_lang, '/fr/') !== false) {
$lang = "fr";
}
else if (strpos($find_lang, '/de/') !== false) {
$lang = "de";
}
else {
$lang = "en";
}
echo $lang;
parse_ini_file($lang . ".ini");

Categories