# Single point of entry for PHP app

This topic is 1868 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I know some people make a single point of entry to their website which is usually index.php - the first site you visit and login from.

But what is this exactly? Is it a controller that decides which webpages to show like in this tutorial http://www.technotas...ont-controller/ ?

Why would you want a controller for a whole page?

How does it work? Does it just redirect every link you click to the index.php and every header in php leads to index.php?

In the provided tutorial, author goes on how we type the url and it redirects us to index.php. But what does this mean in reality, when you must click links and forms to navigate through a website?

Does this mean that every submitted form and every new php page redirects to controller page and then the controller page redirects everything to index.php?

##### Share on other sites

The "rewrite" happens completely inside the web server and isn't visible to the browser or user.

The reason they do it is to make "friendly" URLs. This is possibly a good idea, but I wouldn't waste too much time worrying about it, particularly inside an online game which requires authentication anyway and search engines should not be touching your internal (players-only) pages.

Technically this is a "URL router" - a component which takes the request, breaks it down and sends it to the appropriate piece of code, Django etc, use similar things. Don't worry about it if you're using your own code, do the simplest thing which could possibly work instead.

Mainly just a load of "search engine optimisation" w****** playing with themselves.

##### Share on other sites

But I hear that index.php or so called fron controller makes things act more like a coherent system and less like a loose collection of standalone pages. And you also have a bigger control over your page?

You are correct I am building a browser game, a simple one. And redirecting everything to your front page is supposed to be a good idea for better control over which views/templates to load.

I just don't know how to implement this, I mean right now I access new pages via links or forms. And when the link or form sends a request to a database, should that database return the result to the controller and that controller to the fron controller, or what...

##### Share on other sites

If you have multiple php scripts the url practically translates directly to a file on the server. This is a very rigid design and can cause problems when you decide to extend your web application.

Usually the routing system is done with a query parameter. e.g: index.php?url=posts/byUser/24

In the index.php you would then extract the information from this url parameter and execute the appropriate action.

Most web servers have a rewrite system that can translate urls.

So if the users enters http://www.myserver.com/posts/byUser/24 for example it would be translated to index.php?url=posts/byUser/24 without the user noticing.

I have to disagree with markr here. I find that even for a small website this initial investment quickly pays off.

##### Share on other sites

If you have multiple php scripts the url practically translates directly to a file on the server. This is a very rigid design and can cause problems when you decide to extend your web application.

Usually the routing system is done with a query parameter. e.g: index.php?url=posts/byUser/24

In the index.php you would then extract the information from this url parameter and execute the appropriate action.

Most web servers have a rewrite system that can translate urls.

So if the users enters http://www.myserver.com/posts/byUser/24 for example it would be translated to index.php?url=posts/byUser/24 without the user noticing.

I have to disagree with markr here. I find that even for a small website this initial investment quickly pays off.

But what is so wrong about simply having the URL http://www.myserver.com/posts/byUser/24  I get the part you can make shorter URLs and such but why  index.php?url=posts/byUser/24 instead of http://www.myserver.com/posts/byUser/24 ?

Another thing why would I use this if my user has to log in to access the page anyway? I could simply use $_SESSION variables to display correct data on my index page... like so (I am building a simple browser game): if ($_SESSION['combat'] = true)
{
include 'certain_page.php';
}
else if (...)
{
include 'another_page';
}
else if ..... etc...


##### Share on other sites

I think you need 2 equal signs in your if.

Maybe 5 years ago me and a friend wrote something similar, we used pretty much the piece of code you just posted, but checked the $_GET[](or the other one, cant remember which one is invisible in the URL), so the URL was always just index.php. In the index.php we would check for cookies and all the stuff we needed on every page, we then had all the main divs like top and bottom, which would include top.php and bottom.php. In those 2 files, again we checked what php file to include, ie. in the top.php we'd check if the user is logged in, if yes, show resources, if not show a nice image, in the bottom.php, we'd check the$_GET[] parameter and decided what game page to show, that way you don't need all the redundant code, like checking if the user is logged in on every page.

It may seem like overkill, but it is quickly implemented, also it helps a bit with the error handling, since you can either redirect everything you don't know to a global error page or have a few error pages, which handle different things.

But I don't know if this is still a good idea/design.

##### Share on other sites

If you have multiple php scripts the url practically translates directly to a file on the server. This is a very rigid design and can cause problems when you decide to extend your web application.

Usually the routing system is done with a query parameter. e.g: index.php?url=posts/byUser/24

In the index.php you would then extract the information from this url parameter and execute the appropriate action.

Most web servers have a rewrite system that can translate urls.

So if the users enters http://www.myserver.com/posts/byUser/24 for example it would be translated to index.php?url=posts/byUser/24 without the user noticing.

I have to disagree with markr here. I find that even for a small website this initial investment quickly pays off.

But what is so wrong about simply having the URL http://www.myserver.com/posts/byUser/24  I get the part you can make shorter URLs and such but why  index.php?url=posts/byUser/24 instead of http://www.myserver.com/posts/byUser/24 ?

Another thing why would I use this if my user has to log in to access the page anyway? I could simply use $_SESSION variables to display correct data on my index page... like so (I am building a simple browser game): if ($_SESSION['combat'] = true)
{
include 'certain_page.php';
}
else if (...)
{
include 'another_page';
}
else if ..... etc...


There is nothing wrong with having the URL http://whatever.com/posts/someusername/hello-world , this is what you normally want (it is very search engine friendly)

What you don't want is to be forced to create a folder on the webserver for each user and a file for each post (which is what a webserver normally wants for such a URL to work), thus you use url rewrites to convert such an URL to something like http://whatever.com/index.php?page=posts&user=someusername&post=24 (the visitor never sees this url, its only used by the backend) , you could just as well rewrite it to http://whatever.com/posts.php?user=someusername&post=24, if you throw in for example a spanish translation you can use the url: http://whatever.com/es/mesanjes/someusername/hola-mundo and rewrite to http://whatever.com/index.php?page=posts&user=someusername&post=24&lang=es without any code changes (all thats needed is an extra DB entry for the translated content).

For a game where the user never jumps directly to a specific piece of content you don't need this(as you can use a single url for the whole game), for a normal website however where users should be able to access any content from a search engine each piece of content should have a unique, descriptive and clean url.

Edited by SimonForsman

##### Share on other sites

There *are* some advantages of using "meaningful" URLs; Wikipedia uses them of course (and it's written in PHP). But they aren't a magic bullet, or generally useful.

Most of your users don't care what your URLs are, search engines probably don't, and in the "grander scheme of things", they aren't worth worrying about.

It doesn't make your application "more coherent" to make a single point of entry - that has to be done by programmer discipline. The single "router", will in any case, have to "fan out" to a number of different pieces of logic to handle different types of request.

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
JoeJ
13

• 14
• 10
• 23
• 9
• 50
• ### Forum Statistics

• Total Topics
632639
• Total Posts
3007591
• ### Who's Online (See full list)

There are no registered users currently online

×