0
votes

I'm a beginner of php and am experiencing a weird problem of php code not working on localhost (open the index.php with Chrome on disk D) but working on web server. Here is my index.php. I searched for solutions but do not find one.

<!DOCTYPE html>  
<html lang="en">  
<head>
<meta charset="utf-8" />  
<link rel="stylesheet" type="text/css" href="style.css">
<title>Hello World!</title>  
</head>  
<body>
<h1>Hello World!</h1>
<p><?php echo "Success"; ?></p>
<p>Welcome! This is my personal webpage. </p>
<p style="color:red">This is a red font. </p>
<a href="subpage1.html"><p>Link</p></a>
<p><?php 
    $file = 'people.txt';
    // Open the file to get existing content
    $current = file_get_contents($file);
    // Append a new person to the file
    $current .= "John Smith\n";
    // Write the contents back to the file
    print file_put_contents($file, $current);
    echo "finished";
     ?></p>
</body>
</html>

The style.css is just some basic formatting of <h1> and <p>.

In addition, there is a person.txt file on the same directory with index.php.

So on localhost, Chrome browser does NOT print "Success", update my person.txt, or print "finished". However, when I upload the index.php, person.txt, and style.css to my web server using ftp, all the php codes are working. Can anyone help me on this?

P.S. The website is here.

3
Have you installed PHP?user2363448
Do you have the correct web server running on your localhost?Chitowns24
Please mention the package you are using e.g. wamp server...iNDicator
Either you don't have a server installed, or the server is no properly configureduser2672373
No Chrome is a client, to use PHP you need to setup a webserver locally and have a PHP server runningChitowns24

3 Answers

2
votes

Here is an option as Chrome is just a web browser that doesn't have a PHP server. I use this for my local PHP and webserver

http://www.apachefriends.org/en/xampp.html

0
votes

Have you got PHP or a web server installed on your localhost? If you do, then it may be down to the settings on your localhost.

0
votes

I hope you have installed wamp or xampp. if you are on windows. put your code inside root or htdocs folder. access your code using

                   http://localhost/yoursite/index.php

if you are getting any error then post it here so it will be easy to guess the issue in your code