Cannot connect to http://localhost/

Hi,

I have started the book ‘Build your own database driven website using PHP & MySQL’.

Following the books instructions I have installed separately,MYSQL, PHP 5.3.2 and Apache 2.2.15 over Windows 7

Yesterday, as soon as I stopped Skype using port 80, and changed the windows host file to include: 127.0.0.1 localhost connecting the localhost, worked. But not today.

The books ‘Connect’ example returns ‘Database connection established.’ inferring MySQL and PHP and Apache are all running OK.

But connecting to local host fails and I just get the message ‘Index of /’ instead of ‘It works’

Any ideas why I cannot connect to the localhost?

Chris

What’s the filename of the page you’re editing? Try visiting that directly.

Something like: http://localhost/myfolder/myfile.php

You’ll also need to place it in the www or htdocs folder in order to access it as a webpage.

FF,

:tup: I think that the localhost location is the point of contention here. So, was localhost redefined (from htdocs)? Is the file requested in the htdocs folder? Has the DirectoryIndex been changed?

Regards,

DK

Hi,

If I run a php file like the book’s example today.php it runs fine and produces the correct result: Today’s date (according to this web server) is Sunday, March 21st 2010.

But if then go to h**p://localhost/ then I get:

Index of /

* today.php

Hope this helps.

Chris

David Hi,

With my very limited knowledge, I agree with you which is why I am so puzzled.

As per the books instructions I did change the entry to:

<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>

Chris

That’s because your today.php is not the index file or defined as a DirectoryIndex.

If you rename today.php to index.php, your page will load instead of the default directory listing page (which is what you’re seeing now)\

The DirectoryIndex value is responsible for finding a page to load when a directory is visited. If there’s no available page to act as that default page, the generic directory listing appears.

Force Hi,

Unless I am not understanding something, my browser is not going to the localhost and not seeing the default index.php.

If I rename the default index.php to today.php that will not run either for the same reason. I am not connecting to the localhost

The frustrating thing is that it was working yesterday.

Chris

Reverse that.

in order for a page to load by going to http://localhost, you either need to have a page named index.php or include the file name for the DirectoryIndex option.

If you just want to access your page, http://localhost/today.php should do it.

Force Hi,

I have the Directory Index set up:

<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>

But the problem is I am not connecting to my localhost and I don’t know why?

Maybe I’m misunderstanding what you’re trying to do. What is it that you’re trying to load?

According to the book and what happened yesterday, when everything is running correctly, when I go to: http://localhost/ i should have a message ‘It works’ confirming the browser has found its way to the default index.php

This is not happening.

I have just discovered that if I stop Apache running and I go to http://localhost I get the message: Firefox can’t establish a connection to the server at localhost

If I do the same with Apache running then I get: Index of /

This makes me think that I am connecting to my localhost, but just not finding the default index.php in spite of having the entry in httpd.conf:

<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>

Why am I not able to read the default index.php?

I may be out in left field, but if you try http://127.0.0.1/
Do you get the same results?

Mitt Hi,

You say “…if you try http://127.0.0.1/ Do you get the same results?”

Answer: Yes, I get the same results as http://localhost/ i.e. Index of /

CL,

Add an .htaccess file to your htdocs directory with

DirectoryIndex index.php index.html

Options -Indexes

The first line will CHANGE the default page to index.php (then index.html if index.php does not exist). The second line will turn off the Directory listing and is, aside from a good thing to do on a production server, will be a good test that you’re in the correct directory.

Regards,

DK

DK Hi,

Sorry, but being a complete novice (but learning fast!), I am not sure what you want me to do.

I do have this entry in my httpd.conf file:

<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>

Chris

CL,

Remove the <IfModule dir_module> and </IfModule>, save and restart Apache.

What I was asking for you to do was to create a nameless text file with an .htaccess extension in your htdocs directory. That’s the EASY way to do the above for your localhost files.

Regards,

DK

Hi, I have re-installed Apache and now I can see the localhost great! Now I can’t get the $_GET function.

In Kevin Yang’s book the first example uses an html file to pass a ‘name’ variable to a php file. The main part for the html file is:
<a href=“welcome1.php?name=Kevin”>Hi, I’m Kevin!</a>

and for the main part of the welcome1.php:
<?php
$name = $_GET[‘name’];
echo 'Welcome to our web site, ’ . $name . ‘!’;
?>
The result I should get is: Welcome to our web site, Kevin!
What I actually get is: Hi, I’m Kevin!

The frustrating part is, more complicated (for me) examples that pass variables using forms do work. Just not the $_GET command.

I read something about turning on 'register_global’s but that did on work.

Any ideas why the $_GET won’t work?

Chris

Well, both files are named “welcome1” and it sounds like you’re seeing welcome1.html when you’re expecting to see welcome1.php

Are you certain the link is OK? I can’t think of a reason why Apache would send a request for a PHP file to an HTML file unless you added some sort of directive somewhere, and I imagine you would remember if you had done that.

And please DON’T rely on register globals. It may save you a few lines of code, but it introduces security risks (as well as other issues). IMHO not worth it.

If you’re sure you’re on the PHP page, try adding a
var_dump($_GET);
to see if it looks like you expect it to.

CL,

That’s a basic PHP setup for a form so I’ll have to ask you to post the CODE of your php script. I’m sure Kevin knows how to do that correctly so I’ll have to compare your script’s code with Kevin’s to point out the errors (or show alternative code).

Mitt’s advice re register globals is spot on! DON’T use those as they are a MAJOR security risk!

Regards,

DK