ERR_CONNECTION_REFUSED - Why can’t your phone find the absolute path of files through localhost?

Asked

Viewed 3,657 times

1

I’m developing a website with framework Bootstrap

During development, I came across the problem related to path (path) of folders, images, pages and etc.

I was using include to include the header and the footer on my site, however, I always found an error on the way, either in header and footer include, in the path of images and/or folders. It was then that I began to read about way relativo and absoluto.

To solve the header and footer path problem, I replaced my includes with file_get_contents, where I save in the variable the absolute address of my website and then only complete with the necessary. In this case I can print the header and footer on any page of my site.

Example:

$a = file_get_contents("http://localhost/sites/nome_da_pasta/include/header.php");

echo $a;

To solve the problem of the path of images, folders and pages, just insert http://localhost/sites/nome_da_pasta/nome_do_arquivo.php within the href tag a.

Before all these changes, I was able to access the site also through mobile, typing in the device Chrome:

IPv4/caminho_do_site/index.php

However, I had the problem of the relative path.

Until then the two methods mentioned above had solved my problem on the site using Chrome browser on the notebook. However, when running the same site on mobile phone’s Chrome or in the notebook’s mozila browser, errors appear:

  • On the mobile appears a message ERR_CONNECTION_REFUSED
  • In mozila only opens the localhost page.

Follow the error print on cell phone: inserir a descrição da imagem aqui

My doubts are:

  • There is a correct way to call the absolute path of images, folders, pages and even footers and headers (being includes)?
  • Is there a better way to view the site on mobile? No need to use Ipv4 (even if the site is only on localhost)?
  • I do not understand how the same site, with the same settings and encodings runs in the notebook Chrome browser, but has difficulty running through Ipv4 on mobile.
  • Why "mobile" cannot find the path of folders/files/images/pages on the localhost if the site (on the notebook) can?

NOTE: I would like to understand the questions I asked above, however, my biggest need is to be able to open the site on mobile also.

Updating:

I downloaded the browser Mozilla on mobile to see if the problem was only in Chrome, the funny thing is that the login page runs normally on mobile, however, when I type email and password, error message appears, follow 2 prints of the test on mobile:

Tela Login: inserir a descrição da imagem aqui

After entering email and password on mobile (keep in mind that the website on desktop all pages open normally):

inserir a descrição da imagem aqui

  • 1

    in place of localhost Voce has to use the IP of the 'server' machine, where your webserver is running in the paths

  • Isn’t it Ipv4? I’m a little lost in this area. I’m running locally, so before I could see through Ipv4.

  • 1

    Have you ever tried to do the include and use the images starting the path by the backslash \? This way, the URL will be related to root of the application. For example include("/sites/silotransce/header.php"). You can also set up for the root be the folder itself silotransce, just to do include("/header.php"), which facilitates the deploy of the application.

  • I’ll try it this way, Anderson. So you think that if I do it this way, I can run on the phone too? Is it "file_get_contents" that is giving problem?

  • I tried and it didn’t work, but I think I figured out the problem and I’m not sure how to fix it. It is noticed that in the link that I am using as absolute path, has the localhost. And in cellular what is used to open is the number of Ipv4. So much so that the login screen normally opens pq in the path still ta the IP, but qnd enters the home page becomes localhost and then gives the connection error. Does anyone know how to put a default IP in place of the localhost on the href link?

  • Do not use file_get_contents for this purpose, use include or require. Using my tip above will solve your problem. If it didn’t work, you must have done something wrong. Post your folder structure and code, please. So we can help you better.

  • 1

    Hello, is it really necessary that the include be done using only absolute paths ? After all, if it is one framework, you can easily access the folder assets or imagens or even any other file on the server without the need to use absolute paths, using only relative paths to possess paths customized, there is here information about the types of paths and some examples, although it is almost a summary.

  • Thank you, after analyzing the information you gave me, I did as Anderson had said before, but in my case it was not necessary to use the "/" bar before the name.php. Edilson, then I realized it really wasn’t necessary.

  • Right, create an answer with the solution used. If you solved your problem using barra, remember that you can always use the DEFINE('DS', DIRECTORY_SEPARATOR), and when you need to write a bar, just type DS, and you won’t have to guess if it’s / or . However I recommend creating make use of paths customized.

Show 4 more comments

2 answers

2


First: don’t use file_get_contents, or use include or require.

The relative and absolute paths are always based on what you defined when setting up your server. In Apache, you can set the path to the DocumentRoot in the apache2.conf. If you are using Xampp, find on c:\XAMPP\apache\conf\httpd.conf. Then change the guideline like this:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot C:/caminho/para/o/site
    <Directory />
            Options FollowSymLinks
            AllowOverride None
    </Directory>
    <Directory C:/caminho/para/o/site>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride None
            Order allow,deny
            allow from all
    </Directory>

Restart Apache. (source: https://stackoverflow.com/questions/10157333/xampp-change-document-root)

What you are doing here is changing the path that apache recognizes when you access port 80 of this machine. That is, localhost. Now, from what I saw in your question, you set up several websites in the same folder. The correct thing would be to create a VirtualHost for every site you have there, or put them in different doors, maybe. Anyway, it is already outside the scope.

The problem you were having with the include was why you were trying to use an absolute path that started before the folder of the site itself. So he was looking for header.php inside the root that is there in Apache, which probably contains the folder site.

1

You need to go on PHP.ini and authorise the include using HTTP.

Search the php.ini for allow_url_include and if you are off change to on, if you are 0 change to 1.

  • I read that authorizing include in allow_url_include is not a good thing to do, as I may have several problems with it in the future.

Browser other questions tagged

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