Testing my php scripts and forms - php

I have two html forms and two php scripts. The first one is a form which is supposed to submit a users email to a .txt file, and the other is a Stripe payment form which uses php code to charge the customer.
Now my problem is that there are some issues with the two php scripts, that I can't figure out how to fix, because I am not really sure how to test the scripts. Normally when testing the html scripts I would just open the html files in my browser, but that doesn't work on my php scripts as the site just shows what is written in the scripts when called/submitted.
So my question is how do I test my scripts, without having to use a hosting account and can I even test it like this?

You need to run a web server on your local computer to test it out. I would suggest looking into something called Vagrant, which allows you to fairly easily create virtual machines on your computer in which you can install anything you like without fear of messing up anything else. If you go here you can even find a "box" to create a virtual machine that already has apache and php installed.

Depending on your version of PHP there's a webserver builtin right into PHP:
http://php.net/manual/en/features.commandline.webserver.php

You probably have to install a web server locally with php enabled. Since php is a server side language it needs a server to run on, in order to send you back the html, after execution. If you need to see the result on a browser.
http://en.wikipedia.org/wiki/LAMP_%28software_bundle%29.
Or run your script from terminal (you must have php installed) if you don't need to see the result in the browser and just want to run your script.
$ php myscript.php

Related

How to open an external GUI application from web browser using PHP?

I'm new to web development. I'm trying to execute a shell script using PHP's shell_exec(). Inside the script, I'm trying to invoke a GUI application(Qt). When I executed the PHP script from a terminal the application started as expected. But when I opened it from browser an empty blank page appeared.
I'm using Ubuntu with apache2 server running as service. When I searched in google, the similar problem is solved in the Windows environment by allowing apache service to interact with the desktop.
PHP Script:
<?php
$log = shell_exec('sh testcmd.sh');
?>
testcmd.sh:
./Program1
Any help provided will be highly appreciated.
It is somewhat unclear what you're asking.
If you wish that browsing to a certain web site will run a PHP script that will open a GUI app for the client to interact with, the answer is "you can't". The reason is that the way the setup works is that the server and the client run on different machines, and your PHP runs on the server machine. As such, the client never gets to see the running program.
The above is true also for Windows. The answer you quote in your question does not apply to a server running on a different machine than the client.
If, for whatever reason, you want something that works only when the server and client run on the same machine (or there is someone watching the server's display), then you need to do the equivalent of the Windows answer.
The graphics display on Linux (assuming you're not running wayland) is using a protocol called X11. In order for the display to appear, your GUI program needs two things. The first is to know which display it needs to use. This is supplied with an environment variable called DISPLAY. The second is an authorization to actually use that display.
So in order for your PHP script to run a GUI app that will show its GUI, you will need to first do the following steps:
Set the DISPLAY variable to the correct value (copy from your desktop environment).
Run xauth add something, where you can get what something is by running xauth list on your desktop environment.
If you do these two things (in this order), your GUI should show up.

Does HTML5 wih php require a webserver?

I am studying on YouTube HTML5 forms with PHP files; the videos either start with a web page containing the form and link to a PHP file or they put everything in php files; I copy the instructors exact files; but they do not work: nothing is posted after filling the form and hitting the submit button.
I have had the files on a USB flash drive then tried them actually on the computer: nothing.
My question is then: "Do I need to have an actual server on my computer in order for PHP files to function?"
Yes. PHP requires a web server to run on.
You can run it on your own computer; the web server doesn't have to be connected to the internet while you are creating and testing your PHP scripts off your local machine. You can read more about this on the official PHP What do I need page
You can download one of the following local servers:
Windows: WAMP
OSX: MAMP
Linux: LAMP
XAMPP is also an option, it's cross-platform (as referenced by ATechGuy)
Here is a good explanation of Why a web server is required to run PHP. Basically it is because PHP is a dynamic server-side scripting language.
However if you just want to run simple PHP scripts, with no web pages. This is possible without a web server running. See this question: How can I run a php without a web server?
Yes, PHP will need to run on a server in order to execute. If you have a Mac, it should be equipped to run an Apache server. I found this article to be really helpful when developing with PHP - https://jason.pureconcepts.net/2015/10/install-apache-php-mysql-mac-os-x-el-capitan/
Yes. PHP files contain code that must be handled by an interpreter, that is, a program that reads the PHP code and outputs accordingly. This can be done without a webserver (using command line php) but PHP is most commonly used with a web server.
You want to setup some sort of stack with a web server and php. A lot of beginners use apache as a web server, and since you are comfortable using youtube for learning, a simple search for "apache php" and your operating system.

PHP exec and OSASCRIPT?

I have a little Apple script as follow:
beep
delay 2
tell application "Finder" to activate
It just makes a sound, wait 2 second and then bring the "Finder" window to the foreground.
When I run it from the command line, it works fine.
Then I want PHP to call that script using the exec() php function.
<?
$cmd = "/usr/bin/osascript \"myscript.scpt\"";
exec($cmd);
?>
It still works fine.
But when I call that same PHP script from the browser, it doesn't work! The PHP starts, the Apple script starts as well since I can hear the beep sound but its last line is not executed.
I thought that would be an environment variable thing so I made sure they were all the same way as in the terminal:
$cmd = "HOME='/Users/mikael' && … && /usr/bin/osascript \"myscript.scpt\"";
The variables are set properly (as check with env|sort) but still no luck with running my apple script inside a php script displayed in the browser and using the standard MacOS apache stuff.
Any idea?
When osascript runs from PHP, through the web server, it's not running with a login context, so it can't send Apple events to applications running on the desktop (like the Finder). You'll find that a similar issue arises if you try to use osascript over SSH.
Login contexts are a complex, poorly documented area of OS X. You may want to get your hands on a copy of Amit Singh's Mac OS X Internals: A Systems Approach if you want to learn more about them.
If you don't, though, the answer is generally pretty simple: don't depend on osascript working correctly from the web server.
OK, I may have found a way that allows the Apple script to be called from PHP from within a browser.
It is not fully satisfactory but this is what I'm going to do:
So basically instead of using the default macOS apache server, I use this one that sets up a web server & also MySQL: http://www.mamp.info/en/index.html
I mention MAMP but there may be other.
my Apple script is finally run fully using that solution.
I had the same trouble and realised that apache executes as user www.
You can change this by editing etc/apache2/httpd.conf . Change user to your user short name and group to staff.

Run a PHP script from multiple computers?

I have written a PHP Script, I run the code like this on MS-DOS on my local PC:
C:\wamp\bin\php\php5.3.0\php.exe index.php
Now I want to upload the index.php on the web but on multiple computers (without PHP/Apache installed), how can I run index.php on ms-dos via web?
If that not possible, I can open multiple putty application on each computer and login ssh?
index.php will continues looping - it wont be good to run script on the browser because it may cause timeout via browser.
You can't. If you're using putty on each computer to log into another machine to run the script, the script will still be running on "the other" machine. All you've done is make its output appear on the local computer that's running putty. It'd be exactly the same as opening multiple shells on the server and running the script in each window.
If you want a PHP script to run "locally", then you need PHP installed locally on each machine that will run the script. And like I said in your other similar question, there are PHP compilers which can produce a .exe that MIGHT be portable, but it's not guaranteed to work. PHP can be compiled, but not all scripts are compileable.
Check out bamcompile.
It lets you compile the PHP file to an exe.
Not everything works but it is worth the shot: http://www.bambalam.se/bamcompile/
I think it uses PHP 4.something so don't know wether it works for your script without seeing it
EDIT
You said you are worried about an timeout if ran in browser.
What does the script do?
Perhaps PHP isn't you're best option.
The easiest way would be to have Apache and PHP installed on those computers, and then have a script visible on that computers that runs the command for you.
exec("C:\wamp\bin\php\php5.3.0\php.exe index.php")
This way, you can start running the script without having the script time out from Apache's end.

How to package PHP apps so they will work on any user computer?

Is there a way to distribute a PHP program like a desktop app, so that even if the user of the app doesn't have a local server environment setup on there PCs, the app will still work?
I was thinking, if there was some type of portable server environment program which includes PHP/Apache, then we could create a batch file which when clicked would start up apache in the portable program and launch the PHP app in the user's default browser. That way PHP apps could be distributed like desktop apps.
But the question is, is there such a portable server environment program that can be used for this?
Yes, if you google for XAMPP portable, you can find several versions of fully portable, fully functioning xampp servers for the go. You would have to make sure it contains all the security settings and extensions you need.
The file your user launches should be an .html file to be sure it opens in your browser. As you need to point the user to his own localhost to run your app (otherwise PHP won't be executed and he'd see a plain html file), you'd have to create a redirect, possibly using javascript or a simple "click here to start" link.
The XAMPP only provides half of the solution:
In addition to the XAMPP you can use Phar files that give you the ability to package your PHP applications as a unit for installation and/or deployment.
You can configure a separate php file which will handle any database creations and initial configuration which can be set to run on the first time.
For a helpful start guide consider the following link:
http://phpmaster.com/packaging-your-apps-with-phar/
Finally if you want to make it seamless (easier) to the user, consider writing a Batch script which will handle running some of the task required (Such as starting apache and mysql and running run.php). Take note of using relative rather than absolute directories to make script development easier.

Categories