Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8086537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:29:14+00:00 2026-06-05T18:29:14+00:00

this is my first attemp to create a frontController and I’ve come over some

  • 0

this is my first attemp to create a frontController and I’ve come over some little problem.

First let me give you an impression how my site looks like:
When I open http://mysite.com/test in my browser, the server will call an index.php with the GET parameter $controller (test). This is done by a .htaccess.
In the index.php there shall be created an instance of the class $controller.
For this I use the __autoload function of PHP.
So the code looks like this:

$controller = $_GET["controller"];

function __autoload($controller)
{
    include("controllers/$controller.php");

    if(!class_exists($controller, false))
    {
        eval
        ('
            class '. $controller . '
            {
                public function __construct()
                {
                    include("404.html");
                    exit();
                }
            }
        ');
    }
}


$application = new $controller;

When you looked at the code I provided you may have noticed the eval() thingy in it.
I use this to avoid the fatal error when a $controller class doesn’t exist, and display a 404 instead of it.

And here starts the fun: when somebody enters an url like

http://mysite.com/ImATroll!:D

There will be

Parse error: syntax error, unexpected ‘!’

And…

Fatal error: Class ‘ImATroll!:D’ not found

So the question is: How can I catch this? How can I escape the $controller variable a way that it only contains characters allowed for classnames?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:29:15+00:00Added an answer on June 5, 2026 at 6:29 pm

    You can actually just throw an exception from your autoload function (p.s.: use spl_autoload_register instead).

    This exception is catchable, so you can provide the ‘default’ functionality.

    This is by far the superior method, but if you insist on creating this fake controller, you should be using class_alias instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first attempt to create a GUI in MATLAB. I haven't been
I'm trying to create a simple threading procedure (granted this is my first attempt
First attempt to use this cool site - after searching for 2 hours: So
I am trying to create 3-tier winform application. Since this is my first attempt
This is my first attempt to create a basic list (i need this at
this if my first attempt at using streaming for WCF, and I am struggling
this is my first attempt at a responsive design so excuse me if this
Because this is my first attempt at an extension method that seems quite useful
This is pretty strange (admitedly, this is my first attempt with python / sqlite),
Preface I've rarely ever been a JS developer and this is my first attempt

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.