I'm trying to learn php and step one is getting php working in some capacity. I'm attempting to use MAMP but I'm having some trouble.
Specifically: if I create a file with the below code and save it as index.html in MAMP's "Document Root" directory, I get a blank page when pointing my browser at http://localhost:8888/index.html.
Code:
<html>
<body>
<?php
echo "Hello World!";
?>
</body>
</head>
Alternatively, if I put a bit of php into its own file (say test.php) and then point my browser at this file, it just displays the full text of the file in the browser.
Any ideas what I might be doing wrong?
I had the similar issue.
Make a new file in TextWrangler or Komodo, or whatever, and add the folllowing code:
AddType application/x-httpd-php .html .htm
AddHandler application/x-httpd-php .html .htm
You're going to save the file as .htaccess (with the dot in the front; this is the file name).
Save it in /Applications/MAMP/htdocs. This is the same place you'll save your php and html files. This .htaccess will be an invisible file; you will not see it in Finder, tho you can if you cd to it in Terminal, or searching w/in Finder and choosing the File Visibility type under Kind.
Now try going to localhost:8888/ and you should see all of the available files there. And with this newly created .htaccess file, you can now embed php inside an html file too.
In MAMP, edit the file:
/Applications/MAMP/conf/apache/httpd.conf
and then search for '#AddHandler type-map' (exclude quotes). Below that, add,
AddHandler application/x-httpd-php .php .html
Save the file and stop and re-start MAMP. Php parsing will occur in files ending with the extensions: .php and .html.
You must save a file with PHP inside it with a .php extension. So you would need to name it index.php instead of index.html. Simple fix.
So, this just worked for me:
instead of having:
MAMP/htdocs/folder-that-contains-all-files/
put all your files directly in the htdocs folder!
so:
MAMP/htdocs/all your files including index.php etc.
Hope that helps!
Modifying /Applications/MAMP/conf/apache/httpd.conf
searching for #AddHandler type-map
and inserting AddHandler application/x-httpd-php .php .html
worked for me.
Related
Just wanted to know what code I have to put in my .htaccess file so my html pages read php that im going to put in there, (just a contact form), I did find it once but can't remember, sorry and thanks.
You have to convert your html files to php
put this line in your .htaccess if you're using apache2
AddType application/x-httpd-php .html .htm
but make sure that your .htaccess file is in the root directory of the website
If you want PHP in HTML files to be interpreted just add this line to your .htaccess file:
AddType application/x-httpd-php .html .htm
HTML Pages don't read your PHP. Instead Apache is reading your file and executes it with a PHP interpreter if some PHP is in there.
Just in case you have a debian server with apache2 you can apt-get install php5. But there is a lot more work to do, for example FCGI and so on.
My problem started when I was working with the .htaccess file to a directory trying to get php to run inside of html files. I tried ALOT of combinations of AddTypes and AddHandlers. I took them out and now my php files are being downloaded by the broswer instead of running. There are a few other questions on here that I have studied up and down, but they are not exactly my problem.
I'm fairly lost now, I've tried just about every combination of AddType and AddHandler. I've since decided to use mod_rewrite and just have all my files end in .php but I can't get them to run.
This is what I'm using to test:
<!DOCTYPE html>
<body>
<?php
echo("php working");
?>
</body>
</html>
Am I missing something? Luckily I made all my changes at a separate directory than the root so that the site still runs, but none of the php files in that folder can be viewed in broswer. What can I do? Any help is appreciated.
EDIT:
Just to make it clear, I was originally trying to be able to put php in my html files so I was messing around with handlers and addtypes. My server does have php. If I call a php script from a .html file, it runs. I just can't get a php file like the example I included above to open in brower ANYMORE, it used to open in browser. My fear is that I have messed something up by trying all the different handlers in .htaccess. I have since cleared my .htaccess file in hopes that would at least get me to square one, but it hasn't.
SECOND EDIT:
I went to the root directory and made a php file and it ran just fine so its definitely something I changed in the .htaccess for the particular directory. Is there anyway I can reset that directory?
have you made sure to install php correctly within apache?
if you are using php as a module, you need the following:
LoadModule php5_module "c:/php/php5apache2.dll"
AddType application/x-httpd-php .php
# configure the path to php.ini
PHPIniDir "C:/php"
or as a CGI binary
ScriptAlias /php/ "c:/php/"
AddType application/x-httpd-php .php
# For PHP 4
Action application/x-httpd-php "/php/php.exe"
# For PHP 5
Action application/x-httpd-php "/php/php-cgi.exe"
other things to consider, are the permissions set correctly on the php binaries, is the php.ini present and correct, have you restarted apache since installing php into apache?
lastly, php doesnt run within html files, you would need to set the following as pretty much the last thing in the apache config
AddType application/x-httpd-php .html .htm
Do you see the line like this in your htaccess file?
php_flag engine off
If yes, delete it.
One more thing that may help you understand what happened
The following piece of htaccess would make Php from being executed but rather downloaded as a text file
<VirtualHost *>
ServerName sourcecode.testserver.me
DocumentRoot /var/www/example
AddType text/plain php
</VirtualHost>
One more thing
I'm guessing that this topic was addressed once here-> htaccess downloading file instead of loading
I'm working on a job at work where I need to create a web forum. In my stupidity I decided to use javascript, not knowing beforehand that javascript is a client-side language. I need a way to save the data from the javascript onto the server and then be able to read the data. I tried looking at things like node.js, however I would have reconfigure the entire web server (which isn't mine) in order to do this. The other solution is to use php. Here's the problem: There's a bunch of includes used in the html file that set up the layout of the webpage (i.e. css files, html files, and even a php include). I can't change the name of the index file to index.php because it breaks all of the includes inside of the file. So I need a way to save, say a text file, using javascript and html. If there's a way to, I would like to do something very simple, like include a php file in the html and then call a php function in my javascript code to get the contents of the file into my index.html page. I thought there was a way to call a simple command like this:
<script>
var thedata = <?php getData() ?>
</script>
where the php function getData() would return a json encoded string with all of the data in it (handled from a separate php file). Is there any way to do this? Any other suggestions for how to handle data storage on a server without changing my index.html file to index.php?
Note: I tried accessing the apache httpd.conf file and adding a handler to pre-process .html files as php files, but that doesn't seem to work (nothing as simple as echo 'test' works on the html file).
Add this to your .htaccess:
AddHandler application/x-httpd-php5 .html .htm
source
It causes Apache to treat HTML files as files that contain PHP. Be careful not to somehow accidentally use PHP syntax in a regular HTML file, though.
Remember that you'll still need to use PHP tags to enter PHP mode. This works as expected:
<p>html content ... <?php echo 'hello, world'; ?></p>
But this will output the the echo command:
<p>echo 'hello, world'</p>
If can make your webserver process your pages thru the mod_php.
if you are using apache just add this to your .htaccess
AddHandler x-httpd-php .html .htm
AddHandler php-script .php .html .htm
AddHandler php5-script .php .html .htm
AddType application/x-httpd-php .htm
AddType application/x-httpd-php .html
I hope you understand the repercussion of doing this. all your pages will be processed like that and the memory/cpu use of your pages will be way greater.
if this is to happen inside one single file, make sure to add it inside a statement.
and within your example you should add:
<script>
var thedata = <?php echo getData(); ?>
</script>
The file extension must be .php, so that the server knows to parse the file.
I have a quick question about changing the file types PHP parses. This website gave this line:
AddType application/x-httpd-php .php
But I'm not clear what file this goes into. Any help would be appreciated - thanks!
That line goes into a file called .htaccess, that changes the Apache server configuration for the folder it is on, and all its subfolders (Unless otherwise specified)
Your server should parse php files with the .php file extension by default though. You could use that to add custom file formats for example.
To parse .mp4 files, like you said in the comments, add to your .htaccess:
AddType application/x-httpd-php .mp4
You need to write that in your .htaccess file.
That makes Apache parse .php files through the PHP interpreter.
You can either add that to your .htaccess or httpd.conf file, depending on what you have access to.
I have been searching on forums and google for hours with no definitive answer other than "Your host may not support PHP". I have dumbed down my PHP page to simply display some text for testing purposes but I am still only getting a blank webpage. I have tried opening it locally into a browser and also through the web server, same result for both. Anyone have an idea as to why this simple PHP wont even show up on the webpage?
<?php
echo "Show some text";
?>
also tried:
<html>
<?php
echo "Show some text";
?>
</html>
What is the filename of your file? Is the file extension a .html file, or a .php file? If it's .html, rename it to .php and test with your first test again.;
To check and see if php is supported on your server, create another file titled: phpinfo.php and insert the following code in it:
<?php
phpinfo();
?>
and save it, and then go to it on your server. You should see the PHP configuration output on that page. If it doesn't work, then you do not have PHP correctly installed on your server.
Your file must be ending in .phtml or .php to make PHP work on a HTML page.
index.php
<?php
echo "Hello World!";
?>
Or you can trick your server, Apache and Nginx support this as does IIS. Basically you can have "index.x" or "index.px" - your own custom extension, it is not advisable for obvious reasons. In Apache (your public_html folder) enable this in your .htaccess file:
AddType application/x-httpd-php .php
AddType application/x-httpd-php .ac1d
AddType application/x-httpd-php .php4
AddType application/x-httpd-php .phtml
AddType application/x-httpd-php .x
# your custom extension above.
To view the source of everything (no processing):
Most server are setup with a source-viewing format already by changing the file name to use the .phps extension. If not, you'd have to create a .htaccess file and temporarily add this line to it:
AddType text/plain .php
AddType text/plain .html
That should force the files to be displayed as plain text whenever they're accessed. Just remember to remove it later.