Restricting Access to php Page with Port

Asked

Viewed 112 times

1

I am doing a login environment and I would like this page to only answer an access port previously configured as "8080". I am using an apache server and it will be installed by me on the Linux machine.

Would this affect another site if maintained on the same apache server? No relation to this login system?

1 answer

1


The first thing is to make sure that your web server is set to answer on the port you wish to have the login.

As you commented that you use Apache, the relevant configuration is here:

https://httpd.apache.org/docs/current/bind.html

Basically, to answer on different doors, you can adjust the listen:

Listen 80
Listen 8080

Or if the application meets different Ips and different ports:

Listen 192.0.2.1:80
Listen 192.0.2.5:8080

Remembering that if the application of one port is different from the other, you can configure virtualhosts (Listen has to be open to all doors it will use):

<VirtualHost *:80>
    DocumentRoot /www/caminho_para_o_site
</VirtualHost>
<VirtualHost *:8080>
    DocumentRoot /www/caminho_para_o_painel
</VirtualHost>

https://httpd.apache.org/docs/2.2/vhosts/examples.html

In case you don’t want to complicate, and the panel will be hosted next to the main website, you can use the information at $_SERVER['SERVER_PORT'] to know if the door used is the correct one:

See a simplified example:

<?php
   if( $_SERVER['SERVER_PORT'] != 8080 ) {
       echo '<h1>Porta n&atilde;o autorizada</h1>';
       // faz o que quiser aqui, no caso da porta estar errada
       // pode simular um 404, ou redirecionar pra onde achar melhor

       die(); // Importantissimo garantir que o script termine aqui
   }

   ... aqui continua sua página ...

Now, if you set up this part of the site to serve only on the 8080, the test itself is unnecessary. You need to analyze within your concrete situation to determine the best way.

  • I believe that it would be much safer for this page to answer only in a certain port, could you explain to me even though the question has been misspelled? I thought this was done via PHP.

  • @Alechaito I would have explained, but as it depends on the server of pages you use and the hosting, I would not know what to add without having more details. For example, which server is it? Apache, IIS, Nginx, or what? Each one is configured in a way. And that is if you have access to the settings, otherwise you have to ask for the support of the hosting company. If you want, as you only have my answer yet, click "edit" on the question and add these details, I’ll see if it’s within my power to complete the answer.

  • @Alechaito gave an improved answer, see if it helps.

  • Thank you very much That’s what I was wondering.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.