PHP script fails to get correct home page - php

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.

Related

How can I prevent double checks in this function?

Here is a code to search and return the existing files from the given directories:
<?php
function getDirContents($directories, &$results = array()) {
$length = count($directories);
for ($i = 0; $i < $length; $i++) {
if(is_file($directories[$i])) {
if(file_exists($directories[$i])) {
$path = $directories[$i];
$directory_path = basename($_SERVER['REQUEST_URI']);
$results[] = 'https://' . $_SERVER['SERVER_NAME'] . str_replace($directory_path, "", $_SERVER['REQUEST_URI']) .$path;
}
} else {
$files = array_diff(scandir($directories[$i]), array('..', '.'));
foreach($files as $key => $value) {
$path = $directories[$i].DIRECTORY_SEPARATOR.$value;
if(is_dir($path)) {
getDirContents([$path], $results);
} else {
$directory_path = basename($_SERVER['REQUEST_URI']);
$results[] = 'https://' . $_SERVER['SERVER_NAME'] . str_replace($directory_path, "", $_SERVER['REQUEST_URI']) .$path;
}
}
}
}
return $results;
}
echo json_encode(getDirContents($_POST['directories']));
So you can pass an array of file addresses and directories and get what ever files inside those directories, Note if you pass a file address instead of a directory address the function checks if there is such a file and if there is it returns its address in the result .
The issue is for the directories it works fine but the files repeat twice in the result and for each file the function double checks this if statement in the code:
if(is_file($directories[$i]))
Here is a result of the function note that contemporary.mp3 and Japanese.mp3
has been re checked and added to the result.
How can I solve this?
If $directories contains both a directory and a file within that directory, you'll add the file to the result for the filename and also when scanning the directory.
A simple fix is to check whether the filename is already in the result before adding it.
<?php
function getDirContents($directories, &$results = array()) {
foreach ($directories as $name) {
if(is_file($name)) {
$path = $name;
$directory_path = basename($_SERVER['REQUEST_URI']);
$new_path = 'https://' . $_SERVER['SERVER_NAME'] . str_replace($directory_path, "", $_SERVER['REQUEST_URI']) .$path;
if (!in_array($new_path, $results)) {
$results[] = $new_path;
}
} elseif (is_dir($name)) {
$files = array_diff(scandir($name), array('..', '.'));
foreach($files as $key => $value) {
$path = $name.DIRECTORY_SEPARATOR.$value;
if(is_dir($path)) {
getDirContents([$path], $results);
} else {
$directory_path = basename($_SERVER['REQUEST_URI']);
$new_path = 'https://' . $_SERVER['SERVER_NAME'] . str_replace($directory_path, "", $_SERVER['REQUEST_URI']) .$path;
if (!in_array($new_path, $results)) {
$results[] = $new_path;
}
}
}
}
}
return $results;
}

Redirect to subdomain with all path and params after domain

How can I redirect user from one page to the same page, but in subdomain?
http://test.com
to
http://subdomain.test.com
http://test.com/page/page2/123
to
http://subdomain.test.com/page/page2/123
https://test.com/catalog?r=44&color=red
to
https://subdomain.test.com/catalog?r=44&color=red
So I want redirect and save all what present after subdomain.test.com in my example. How can I do it?
public function test($request)
{
$subdomain = Market::first()->subdomain;
$domain = $this->get_domain($request);
$protocol = $this->get_request_protocol($request);
$to = $protocol . '://' . $subdomain . '.' . $domain; // all path and params?
return Redirect::to($to);
}
private function get_domain($request)
{
$parts = explode('.', $request->getHost());
$i = count($parts)-1;
return implode('.', [$parts[$i-1], $parts[$i]]);
}
private function get_request_protocol($request)
{
if ($request->secure()) {
return 'https';
}
return 'http';
}
use this
private function get_back_path($url, $domain, $subdomain)
{
$parts = parse_url($url);
$url = $parts['scheme'] . '://' . $subdomain . '.' . $domain;
if (isset($parts['path'])) {
$url .= $parts['path'];
}
if (isset($parts['query'])) {
$url .= '?' . $parts['query'];
}
if (isset($parts['fragment'])) {
$url .= $parts['fragment'];
}
return $url;
}
You can use redirect()->away() like redirect()->away('http://example.com?id=2);

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);
}
}
?>

Correctly source frontpage in PHP function

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 ?

codeigniter - redirect to https and www causes infinite redirect loop

I'm trying to force redirect to www with https from index.php but i keep getting an infinite redirect loop.
Here is my code:
index.php:
$configs = include_once($_SERVER['DOCUMENT_ROOT'] . '/application/config/maintenance.php');
if (stripos($_SERVER['SERVER_PROTOCOL'], 'https') === false && $configs['protocol'] == 'https') {
$protocol = 'https://';
$protocolRedirect = true;
} else {
$protocol = 'http://';
$protocolRedirect = false;
}
if (strtolower(substr($_SERVER['HTTP_HOST'], 0, 4)) !== 'www.') {
$url = $protocol . 'www.' . $_SERVER['HTTP_HOST'];
$wwwRedirect = true;
} else {
$url = $protocol . $_SERVER['HTTP_HOST'];
$wwwRedirect = false;
}
if ($_SERVER['REQUEST_URI'] != '/') {
$url .= $_SERVER['REQUEST_URI'];
}
if ((isset($wwwRedirect) && $wwwRedirect == true) || (isset($protocolRedirect) && $protocolRedirect == true )) {
header('Location: ' . $url);
exit();
}
maintenance.php:
return array(
'protocol' => 'https'
);
Any idea why?
Note: server is running Nginx - but i don't want to redirect thorugh it.

Categories