Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

frankhosaka's avatar

I need a help to my code in MVC PHP work in Hostinger

My host is not VPS. Today I create this code:

index.php

<?php
require 'Controllers/router.php';
$router=new Router();
$router->run();
================================
Views/login.php

<!DOCTYPE html>
<html lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Projeto PHP</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
<style>
html,body {height: 100%}
body {display: flex;align-items: center;padding-top: 40px;padding-bottom: 40px;background-color: #f5f5f5;}
.form-signin {max-width: 330px;padding: 15px;}
.form-signin .form-floating:focus-within {z-index: 2;}
</style>
<body class="text-center">
<main class="form-signin w-100 m-auto">
  <form method=post>
    <h1 class="h3 mb-3 fw-normal">Projeto PHP</h1>
    <div><?=$mensagem?></div>
    <div class="form-floating">
      <input type="email" class="form-control" name=email 
        placeholder="[email protected]" value='<?=$value?>' autofocus required>
      <label for="floatingInput">Email</label>
    </div>
    <div class="form-floating">
      <input type="password" class="form-control" id="floatingPassword" placeholder="Senha" name="senha" required>
      <label for="floatingPassword">Senha</label>
    </div>
    <button class="w-100 btn btn-lg btn-primary" type="submit">Entrar</button>
  </form>
</main></body></html>
=============================================================
Controllers/router.php

<?php
class Router {
  private $routes = [],$controle;
  public function __construct(){
    require 'Controllers/controle.php';
    $this->controle=new controle();
    $this->addRoute('GET','/',function(){
      $this->controle->inicio();});
    $this->addRoute('POST','/',function(){
      $this->controle->login();});
    $this->addRoute('GET','/orcamento',function(){
      $this->controle->orcamento();});
    }
  
  public function addRoute($method, $path, $handler) {
    // $method is the HTTP method (e.g., GET, POST, PUT, DELETE)
    // $path is the route path (e.g., '/', '/about', '/contact')
    // $handler is the route handler (a callable function or object)
    $this->routes[$method][$path] = $handler;}
  public function run() {
    // Get the current request method and path
    $method = $_SERVER['REQUEST_METHOD'];
    $path = $_SERVER['REQUEST_URI'];
    // Find the matching route
    $route = $this->routes[$method];
    if (!array_key_exists($path, $route)) {
      throw new Exception('No route found for method ' . $method .
       ' and path ' . $path);}
    // Call the route handler
    $handler = $route[$path];
    $handler();}}
===============================================================
Controllers/controle.php

<?php
class controle{
    private $conexao;
    public function __construct(){
        require_once 'Models/conexao.php';
        $this->conexao=new conexao();
        return $this->conexao;}
    public function inicio(){
        date_default_timezone_set('America/Sao_Paulo');
        $inicio=date('H:i');
        return $this->conexao->render('login',['mensagem'=>$inicio,'value'=>'']);}
    public function login(){
        $email=$_POST['email'];
        $senha=$_POST['senha'];
        unset($_POST);
        $row=$this->conexao->unico("select * from users where email='$email'");
        if(!$row||$row==0){
            $this->conexao->render('login',
            ["mensagem"=>"Email $email incorreto!","value"=>$email]);exit;}
        $dbSenha=$row['password'];
        if(!password_verify($senha,$dbSenha)){
            $this->conexao->render('login',
            ['mensagem'=>'Senha incorreta!','value'=>$email]);exit;}
        session_start();
        $_SESSION['nome']=$row['name'];
        $_SESSION['id']=$row['id'];
        unset($_GET);
        $this->conexao->render('menu');exit;}
    public function orcamento(){
        echo "projeto em andamento...";
    }
}

It works very well in my local machine, after active PHP server by comand php -S localhost:8000 but in Hostinger i get HTTP ERROR 500 this page not working.

I need change hosting plan or I missed something in the way?

0 likes
8 replies
JussiMannisto's avatar

500 is an http code for a generic server error. There can be many causes. You should check the logs for more information.

But why are you trying to write your own MVC framework? If you have trouble with the basics, I recommend you learn Laravel first. You'll learn a lot more about software architecture and the MVC pattern. You'll also learn how PHP code is typically formatted: it has a very different syntax to Python and you shouldn't try to treat it the same or it becomes hard to read.

Your app is vulnerable to at least SQL injection and CSRF attacks. Guess what happens if someone attempts to log in in with this "email":

'; drop table users;--

Criminals/bots could also do much more nefarious things, such as change passwords of existing users and then log in as them. Laravel has safe guards against basic attacks like this.

There are lots of resources online on how to build apps using Laravel. Later it can be a fun excercise to write your own bare-bones MVC framework. I've also done that in the past. But I don't recommend it early on if you want to develop your skills.

jlrdw's avatar

Bind those parameters.

Edit:

Please at least take the free PHP for beginners course right here on this site.

frankhosaka's avatar

Well, I don't know where I find basic PHP with MVC (without composer, without Simfony facilities) that runs in a site hosting like Hostinger. My true wish is code a MVC without router. Is it possible?

JussiMannisto's avatar

@frankhosaka I've never used Hostinger, but a google search returned many articles on how to deploy a Laravel app and how to use composer there.

MVC web app without routing doesn't make sense to me.

frankhosaka's avatar

I have Laravel, but I can't install Breeze, Chirps, Tailwind, LiveWire, and so on, because they need a NPM perfomance, but it is impossible in a site hosting, only in VPS hosting. So I dreamed with MVC without Simfony, to avoid use Composer in Hostinger. Well, in Hostinger and in Laracasts all is impossible. I apologize for taking up your time.

jlrdw's avatar

I'm pretty sure Hostinger has a how to article on setting up laravel.

frankhosaka's avatar

Yes, Hostinger has VPS Hosting where you can use NPM without any limitation, but many people use Site Hosting, it's very limited, and I choose the most limited capabilities.

Now, I tried a MVC PHP with Simfony (https://sd.blackball.lv/en/articles/read/19058-how-to-build-a-simple-php-mvc-framework), I used Composer in my local computer, and upload all in Hostinger, and the tutorial works very well. I will use as my template in near future. Laravel is fantastic, but it has 9.000 files, meanwhile MVC has a hundred files - it is perfect to who has no need a framework, but has a smartphone and want private information.

JussiMannisto's avatar

@frankhosaka MVC is not a piece of software, it's a design pattern. Laravel is an MVC web app framework.

The number of files in Laravel is not a real performance issue. Only the files that are needed will get loaded.

You don't necessarily need npm on the production server to run a Laravel app. You only need it during the build process, so you can build the assets locally and just upload the files.

That said, I personally wouldn't run a Laravel app in that kind of environment. Or any other non-trivial website for that matter.

Please or to participate in this conversation.