Php mvc not loading images using <img> - php

I have made my own php MVC and it works perfectly for everything except when it comes to loading certain images. For example it will load like icons, and some other images but when it comes to using the tag it wont load the image but when i look in the google chrome developer inspector tool it shows that my tag images aren't loading. Its giving back a 301 number and loading the HTML. So in terms its like my MVC is thinking the URL for the image is a page!I want my MVC to only make requests to my index.php file which is in the root directory for my site. Also when it comes to it generating the HTML code i mean that it going to my error html page when i put the direct image url
in the browser.
Absolute URL for the image:
http://localhost/website/user_data/cfedabd2e283c19c06f3b8b5bd45df4bb66c2b3a/photo.jpg
Here is my code below:
.htaccess--
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?u=$1 [PT,L]
</IfModule>
bootstrap.php(Router)--
class Bootstrap {
function __construct() {
$url = $_GET['u'];
$url = rtrim($url, '/');
$url = explode('/', $url);
if (empty($url[0])) {
require 'controllers/index.php';
$controller = new Index();
$controller->index();
return false;
}
$file = 'controllers/' . $url[0] . '.php';
if (file_exists($file)) {
require $file;
} else {
$this->error();
}
$controller = new $url[0];
$controller->loadModel($url[0]);
// calling methods
if (isset($url[2])) {
if (method_exists($controller, $url[1])) {
$controller->{$url[1]}($url[2]);
} else {
$this->error();
}
} else {
if (isset($url[1])) {
if (method_exists($controller, $url[1])) {
$controller->{$url[1]}();
} else {
$this->error();
}
} else {
$controller->index();
}
}
}
function error() {
header('location: ' . APP_URL . 'index');
}
index.php(Where i want all the requests to go)--
<?php
//error_reporting(E_ALL);
session_start();
/* Require all config files */
require 'config/config.php';
/* Initiate system */
$bootstrap = new Bootstrap;
?>
Example What the user sees--
<h3>Error page not found</h3>
The image with
$photo = "http://localhost/website/user_data/cfedabd2e283c19c06f3b8b5bd45df4bb66c2b3a/photo.jpg;
<img src="<?php echo $photo; ?>" width="200" style="max-height: 200;"/>

Related

routing in MVC doesn't work in server, but work properly in localhost

i made a simple mvc with routing system. When i deployed it in 000webhost to be tested, all links don't work. They only shown in the URL. No error message.
i tried to change php version on the server to be the same as my php on localhost, it still didn't work
I guess maybe there's something wrong in my htaccess
here is the routing code:
<?php
class App
{
// controller, method, dan parameter
protected $controller = 'Home',
$method = 'index',
$params = [];
public function __construct()
{
$url = $this->parseURL();
// get controller dari url
if (file_exists('app/controllers/' . $url[0] . '.php')) {
$this->controller = $url[0];
// unset untuk menentukan param
unset($url[0]);
}
// call controller
require_once 'app/controllers/' . $this->controller . '.php';
// instansiasi class controller
$this->controller = new $this->controller;
// get method from url
// check if method exist in url
if (isset($url[1])) {
// cek ketersediaan method pada controller
if (method_exists($this->controller, $url[1])) {
$this->method = $url[1];
// unset untuk menentukan param
unset($url[1]);
}
}
// get param from url
// check array
if (!empty($url)) {
$this->params = array_values($url);
}
// run controller and method and param if exist
call_user_func_array([$this->controller, $this->method], $this->params);
}
public function parseURL()
{
if (isset($_GET['url'])) {
$url = rtrim($_GET['url'], '/');
$url = filter_var($url, FILTER_SANITIZE_URL);
$url = explode('/', $url);
return $url;
}
}
}
and here's the htaccess
Options -Multiviews
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [L]
RewriteRule !^(public/|index\.php) [NC,F]

Instead of using ?id=30

and thanks for taking a look to this weird and awkward post.
I have been working on a Cars website for a while (version: 1) wich is pretty basic, now im learning a little bit more about POO so i guess i will be updating it...
I have seen before on a Scripts website that they do this:
Example:
websiteurl.com/scripts/view/300 or websiteurl.com/scripts/view/1761/media/
or websiteurl.com/scripts/view/1761/comments
Im currently doing this:
mywebsite.com/viewCars.php?id=300 but i would like instead of doing that , doing this:
mywebsite.com/cars/view/300
Thanks, and i hope someone understand my question, forgive me for my bad spelling but english isn't my first language.
Assuming you are using apache, soemthiing like this should give you the format you mentioned
in your .htaccess file
----------------------
RewriteEngine On
RewriteBase /
RewriteRule ^cars/view/([0-9]+)$ viewCars.php?id=$1 [NC,L]
have you heard about htaccess. Check the documentaion. You can sole your problem by using htaccess and some regular expresion. Here is an example solution for your question.
https://mediatemple.net/community/products/dv/204643270/using-htaccess-rewrite-rules
For your solution, you can write following code in your htaccess.
RewriteEngine On
RewriteBase /
RewriteRule ^cars/view/([0-9]+)$ viewCars.php?id=$1 [NC,QSA,L]
If you are using a MVC architecture, you could do something like this:
function __construct() {
$url = isset($_GET['url']) ? $_GET['url'] : null;
$url = rtrim($url, '/');
$url = explode('/', $url);
if (empty($url[0])) {
require 'controllers/index.php';
$controller = new Index();
$controller->index();
return false;
}
$file = 'controllers/' . $url[0] . '.php';
if (file_exists($file)) {
require $file;
} else {
$this->error();
}
$controller = new $url[0];
$controller->loadModel($url[0]);
// calling methods
if (isset($url[2])) {
if (method_exists($controller, $url[1])) {
$controller->{$url[1]}($url[2]);
} else {
$this->error();
}
} else {
if (isset($url[1])) {
if (method_exists($controller, $url[1])) {
$controller->{$url[1]}();
} else {
$this->error();
}
} else {
$controller->index();
}
}
}

Parse_url and ($_SERVER['REQUEST_URI'])

I´m trying to pick up the correct path with parse_url and $_SERVER['REQUEST_URI'] in order to show the correct page, however the problem I´m having is that only my index.php page is showing.
The url to my index.php page should be localhost/mvc-framework which is all correct, but when I take:
localhost/mvc-framework/file.php it is also showing my index.php. Basically anything I type in after somefolder also show the index.php page...
I really hope someone can help me out as I´ve been struggling with this for almost 3 hole days.
I´m probably stupid:/
I have all files in "mvc-framework" and inside I have one index.php file that ties everything together and 3 folders: "controller", "model", "views".
In controller folder I have one file called main.php which is were I´m trying to pick up the url:
<?php
/* controller/main.php
*/
class mainController
{
public $load;
public $urlValues;
public function __construct()
{
$url = parse_url($_SERVER['REQUEST_URI']);
$url = explode('/', $url['path']);
$this->urlValues = array('controller' => $url[1]);
//index page
if ($this->urlValues['controller'] == "mvc-framework") {
$text = array("key" => "Hello");
$this->load = new load();
$this->load->view('index.php', $text);
}
//register page
elseif ($this->urlValues['controller'] == "mvc-framework/test.php") {
$text = array("key" => "Test");
$this->load = new load();
$this->load->view('test.php', $text);
} else {
echo 'error';
}
}
}
In the "model" folder I have one file called load.php which will take the url and point it to the the correct file in the views folder...
<?php
/* model/load.php
*/
class load
{
/* This function takes parameter
* $file_name and match with file in views.
*/
function view($file_name, $data = null)
{
if (is_readable('views/' . $file_name)) {
if (is_array($data)) {
extract($data);
}
require 'views/' . $file_name;
} else {
echo $this->file;
die ('404 Not Found');
}
}
}
In the views folder I have 2 files, one index.php and one test.php...
And here is my bootstrap/index.php file which is in the root folder and bind everything together...
<?php
/* index.php
*/
require_once 'model/load.php';
require_once 'controller/main.php';
new mainController();
try this
$url = explode('/', trim($url['path'], '/'));
and $this->urlValues['controller'] == 'test.php' for request localhost/mvc-framework/test.php
You always get the index.php beacuse of the .htaccess file, which always redirect the request to the frontend controller, index.php. I suppose...

.css file doesn't get carried out

Style.css doesn't get carried out when url is http://example.com/mvc/login/requestsExceeded,
but it does in my login index page that is http://example.com/mvc/login,
if I add forward slash http://example.com/mvc/login/, then it doesn't work either.
mvc = site in subdirectory
login = controller
requestsExceeded = view
.css file is in http://www.example.com/mvc/views/themes/default/style.css
The file path is ok becouse i've tryed like this:
<?php if(file_exists("views/themes/{$theme}/style.css")) echo 'TEST'; ?>
<link href="views/themes/<?=$theme;?>/style.css" type="text/css" rel="stylesheet" />
and it does echo out TEST.
Here my simplified router:
<?php
$controller = "Index";
$action = "index";
$query = null;
if (isset($_GET['load']))
{
$params = array();
$params = explode("/", $_GET['load']);
$controller = ucwords($params[0]);
if (isset($params[1]) && !empty($params[1]))
{
$action = $params[1];
}
if (isset($params[2]) && !empty($params[2]))
{
$query = $params[2];
}
}
$modelName = $controller;
$controller .= 'Controller';
$load = new $controller($modelName, $action);
if (method_exists($load, $action))
{
$load->{$action}($query);
}
else
{
die('Invalid method. Please check the URL.');
}
I'm pretty sure that this is caused by .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?load=$1 [PT,L]
-I would like to restrict all access unless index.php...,
-Allow access to .css,.gz,.js and image files,
-remove the forward slash from url,
-redirect 301 to index.php,
-redirect http://example.com/mvc/home to index.php,
Help would be appreciated!
Change your import to
<link href="/mvc/views/themes/<?=$theme;?>/style.css" type="text/css" rel="stylesheet" />
If you want the same link to work from anywhere in your site, then you'd better let the path be absolute, that is start with /.

How to prevent 403, 400 errors URL forwarding?

Site works good when user enter any url which doesn't exist and forward request to error controller.
But
If you write a script in url site throw 403 error
If you write some asp codes site throw 400 error
How can i prevent this and forward them to custom 403 and 404 pages? I tried forward with htaccess but i couldn't succeed it.
Another problem:
If you write ANY ascii code in adress bar, bootstrap forward to welcome page (controller ->index.php) .How is possible?
Directory structures, htaccess and bootstrap codes are below. Thank you for any help.
Directory structure:
/config
/libs
-bootstrap.php
/controllers
/models
/views
/public_html
-index.php
htaccess
htaccess
htaccess in main directory
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public_html/ [L]
RewriteRule (.*) public_html/$1 [L]
</IfModule>
htaccess in public_html directory
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
index.php in public_html
<?php
define('DS', DIRECTORY_SEPARATOR);
define('ROOT', dirname(dirname(__FILE__)));
require_once (ROOT . DS . 'libs' . DS . 'bootstrap.php');
$app = new Bootstrap();
bootstrap.php in libs
<?php
class Bootstrap {
function __construct() {
$url = isset($_GET['url']) ? $_GET['url'] : null;
$url = rtrim($url, '/');
$url = explode('/', $url);
print_r($url);
if (empty($url[0])) {
require '../controllers/index.php';
$controller = new Index();
$controller->index();
return false;
}
$file = '../controllers/' . $url[0] . '.php';
if (file_exists($file)) {
require $file;
} else {
$this->error();
return false;
}
$controller = new $url[0];
// calling methods
if (isset($url[2])) {
if (method_exists($controller, $url[1])) {
$controller->{$url[1]}($url[2]);
} else {
$this->error();
}
} else {
if (isset($url[1])) {
if (method_exists($controller, $url[1])) {
$controller->{$url[1]}();
} else {
$this->error();
}
} else {
$controller->index();
}
}
}
function error() {
require '../controllers/error.php';
$controller = new Error();
$controller->index();
return false;
}
}

Categories