Running PHP on my machine locally? - php

I download xampp 1.7.7 and it has PHP and MySQL. I bought a book named HeadFirst PHP MySQL. My problem is that I can't run the examples from the book.
The example files consist of 2 files: one HTML, and one PHP file. The example is like this you fill the form from HTML file then send it to a web server then the php will handle the HTML file then return it to you.
When I filled the form then I click the submit button, it didn't return what it was supposed to return. What it returns is the actual PHP code.
Do I need to transfer all the files from the book to a webhost so that I cant try the examples I said? How can I run it locally?
UPDATE!!
I tried what anselm said now it says OBJECT not found... url not found then the 404 error.

Ensure that you're accessing the html file through http://localhost/file.html instead of c:\etc etc.html

if you are developing on windows i really recommend using wamp it really easy to use and include the main stuck which is php, mysql, apache it really easy to install and automatically configures everything you then can find www folder in wamp folder on your c drive where you have to drop you php files
http://www.wampserver.com/en/

Your PHP code isn't being parsed by PHP. Either the Apache config is wrong (unlikely since you installed XAMPP, but you can check by following the steps at http://www.php.net/manual/en/install.windows.apache2.php), or your files aren't really named .php.
In Windows, make sure you have file extension viewing turned on, and ensure the file extensions really are .php.

In your XAMPP Control Panel, make sure that Apache started.

Related

Do I need Xammp for single php file to work?

I'm an absolute beginner when it comes to PHP. I have a standard html/css/js project that I'm editing with VS code and running a development server with the Live Server extension, which is running on localhost:5500.
I'd like to integrate a single .php file into my project which will handle a form submission.
Will I need xampp running on my local machine in order for the .php file to work?
At the moment the form action is sending a post request to my .php file but I get a 405 error from the browser.
Yes, A PHP processor is needed for the PHP code to work. If you have XAMPP installed (which means you have php), you can run
php -S localhost:5501 (make sure C:/xampp/php is in your PATH environment variable)
in the root directory of your project, this will start a PHP server and you don't have to move everything to the htdocs folder.
You need a server capable of processing PHP.
That doesn't need to be Apache HTTPD. If you do pick Apache HTTPD then you don't need to install it as part of XAMPP.
The Live Server extension is not capable of processing PHP so you need a different server. (I think I saw a PHP capable equivalent in the extensions library in VS Code, but I recall it being quite fiddly to configure.)
Yes, you need a server (apache or nginx) + php.
Xampp include all this in one application: x (SO) A (apache) M(mysql) P(perl) P(php)

My .php files not running on WAMP Server, it is trying to save the file instead

I have just cloned a project, so it works perfectly fine when i load it e.g: "localhost/example/contact.html", but when i click to homepage which the endpoint is "localhost/example/index.php" it asks me to save the file like that:
Asking to save the php file
I have checked everything in files such as php.ini, httpd.conf but still cant find a solution, I also reinstalled wamp 2 times.
Also the php error log gives me this error: The file C:/WINDOWS/system32/drivers/etc/hosts does not exists
Any help would be appreciated!
You need to load and enable mod_php or set up php-fpm. Afaik Wamp uses mod_php which is simpler. The reason your web browser asks you to download the php files is because you have not told the web server to treat .php files differently that .txt files.
There are plenty of resources on the interwebs about setting up php and apache:
https://cwiki.apache.org/confluence/display/HTTPD/PHP
Given that you are using Wamp, which should work out of the box, I'd say reinstall one more time and run the installer as an admin. If the issue about the etc/hosts file persists, you can create the file yourself.
However, I don't think the hosts file is necessary to run wamp or to render / run .php files.
PS: Your question is not about programming or code, so it's going to be closed because it fits better on the superuser website. Welcome to SO!

Use PHP Include offline

In testing our site it would be so much easier to use PHP include offline. Essentially rather than put a page together for the purposes of testing, we would rather just include the various html files from their respective areas.
Our macs do not render PHP pages when offline and just show the source code. Is there a way to make them?
You need a local webserver in order to run php in your browser.
check out mamp or zend server
Once installed you can type localhost and whatever is the default directory will show in your browser. What I usually do is instad of just accessing everything through localhost I create an alias in the /private.etc/hosts/ file and the http.conf file in mamp
You would need a local web server to render any PHP code. MAMP might help you : www.mamp.info
To include HTML code into php with include() or require() you'll have to change the extension of the files from .html to .php even if no PHP code is inside them.
Also you could use a localhost server. There are mainly 2 of them for Mac:
Mamp
Xampp
I use the latter and I think it's the best. I had some trouble with Mamp years ago while Xampp worked just fine.
You have to make a php server. Free MAC php/ mysql server http://www.mamp.info/en/index.html

XAMPP (LAMPP) server not serving php files properly, browser tries to download the script & weird behavior

I'm starting a new project using Linux and PHP, but for the early dev state i'm now, I'm trying to use XAMPP for now.
I'm Running Ubuntu 10.04 x64 on a laptop, And got everything I need. The site I should build is based upon CodeIgniter and some implementation of smarty, but I think this is not that important because the same site runs pretty well on windows (with XAMPP )
The Problem I have is, if I put some php file on the root (like foo.php) with phpinfo() on it, the server parsed it as it should be, but when I try to get the url for one CodeIgniter app (as http://localhost/site/site.php ), Firefox tries to download the file & Chrome alerts some server error.
Again, the site as it works perfectly on windows (I share code using mercurial, if that's important)
I have around 3 codeigniter apps sharing the same System folder, and those get pickup by the loader (as site.php, admin.php...), but anyone I pick I still getting the same msg.
Also, the server list all the files perfectly, and tried to rename the file and then asked for the same (to see if the problem is cache or something) but I get a 404.
I'm also a newbie on apache and linux in general, I don't know what to do...
Check your apache.conf and httpd.conf. There are configuration entries called AddHandler which assign a module to each file extension which should handle this. If no module is provided the apache will send the file as plain text to the client.

How to get started with sql and php?

I was wondering how to link a SQL database on my PHP code.
I am using a database on my local server that I set up and I have already created a database, I tried writing a PHP script with ++notepad, but when I clicked on the script, it opened the browser and displayed a blank page. What am I doing wrong? I created my databases using phpMyAdmin.
You need to be running a web server locally which supports PHP (like Apache). It it the web server which does the PHP processing. At the same time you will have to run a MySQL (or any other) database engine too, so your PHP code can connect and query it. (I'm assuming you've already installed PHP.)
I would recommend you install XAMPP which includes Apache, PHP, MySQL and a bunch of other stuff. It one of "Apache friends". Once you install XAMPP you would have a folder htdocs in the xampp folder. Create a folder for you project myproject and place your PHP script script.php in there. Now if MySQL and Apache are running following should behave as you expect.
http://127.0.0.1/myproject/script.php
For the tutorial, to begin with you can use W3Schools. Brief and to the point. They have both a PHP tutorial and a SQL tutorial.
This is a very good book to start with:
PHP and MySQL
Web Development
Luke Welling and Laura Thomson
You have to play with php/sql code practically. Only reading cant help!
go for w3shools as mentioned above :)
start with simple code and then go to the complexity. all the best.. :)
There are multiple ways on how to do that.
One of the most modern ways to do that, is to use PDO to connect to the DB with PHP.
To run PHP on your PC(which i assume is your workstation), you need the supporting software for it, for php to run you need the PHP software installed, and likewise if you want to run the server software you need apache, however you can go for a single install package which will install almost everything for you to run your php script, download and install the wamp.
to install wamp which stands for (windows, apache, mysql, PHP) if you are running windows, and lamp for linux or mamp for mac, all of which are free softwares, you can download wamp from the following link. http://www.wampserver.com/en/download.php that is the first step which you need to do.
step 2: once wamp is installed the default directory of installation is C:/wamp (i reccomend do not change the location of the installation), you need to start the wamp server by just opening or double clicking the software which you just installed.
step3: navigate to the default directory where you should be storing your PHP files and that is C:/wamp/www note that you should be keeping all your PHP files here apart from this location if you store it anywhere else it wont simply run at all, and that it should and must have the .php extension.
step4: create or save the php files in this location(c:/wamp/www) i for example will assume you have created a file test.php in the www folder. now to access this file open your web browser and type http://localhost/test.php there you go, you will have the access to your PHP file (Note: PHP files cannot be run by simply double clicking the file, you have to follow this procedure in order to run the PHP files)
step5: open the test.php in any of your text editor like notepad, or i recommend use an IDE (Netbeans is free and worth a try), and write your code in the following format.
<?php
your PHP code should go here.
?>
when i started writing the code i started by following a nice tutorial which i found in zend.com, it has one of the best examples, and moreover the tutorial is meant for the beginners. hope this helps you..
http://devzone.zend.com/tag/PHP101

Categories