I have two files, main.php and register.php that I've been editing. I renamed their extensions to .txt so that you can view them here. If I had left them as .php's, they'll try to run when you click on the filenames. You'll see that they are nothing fancy, just an initial draft to get you started. So, now I'd like to show you how to execute those files.
I told you my directory name at the beginning of the post. So, type into your browser:"localhost/eclipseworkspace/projectX/main.php"If I had named it "index.php" instead of main, then you could skip the filename. This code shows how you can validate the fields of the form in PHP, and give the user an error message if something is wrong.
It works fine except for one problem: if there are multiple errors, such as a zero length username AND a zero length password, then only one error message is displayed to the user. In this case it's the password error message, since that check occurs later in the code.
To fix this, we can change the code to keep an array of error messages. Here's how we'll do that:1. Instead of setting $error=false, lets instead create an empty array, and let's make the variable plural $errors instead of $error. Of course, you know that a compiler, or in this case the PHP interpreter, does not really distinguish between singular and plural form.
It just helps make the variable names more meaningful for us:In this case where it's just one line of HTML to be output, that's not a bad way to do it. But when sending long stretches of HTML I think the format where you jump back and forth between PHP and HTML is the way to do. You end up with a file that looks more like HTML code with PHP sprinkled in, rather than an onslaught of PHP echo statements.
That might be hard to visualize with just a small example like this, but if you code up an entire page you'll see what I mean.The idea of echo'ing all output from a PHP program would be similar to writing a C program where you printf all the HTML. Come to think of it, you could actually write all this pure C. You could have an Apache module that links in your C code as a shared library.
It would handle the TCP connection from the browser, doing printf's to send the HTML and other content to the browser. In other words, you'd be generating the HTML from your C program entirely. All the things that we are doing with PHP, you could do it with C instead in which case you would not need to switch between PHP and HTML. However, it'd be more complex. Say you needed to make a change, with PHP you just edit the file and you are done; with C, you have the extra step of compiling which will slow you down. Then you have to restart the web server process.
Showing posts with label php. Show all posts
Showing posts with label php. Show all posts
Saturday, July 2, 2011
Thursday, June 30, 2011
Upload a File to a Remote Server with Phonegap
I recently started looking at PhoneGap as a way to build native smartphone applications without having to learn another programming language. The process, so far, has been going very well. I’ve been able to build a basic application with very little trouble. I did, however, hit a big hiccup in development when I decided to try and upload a file from a phone to a remote location.
The unfortunate part of this hiccup was that I think its primary cause is rooted in poor documentation.In this post, I want to explain how I was able to upload a photo from a phone, using PhoneGap (and PHP on the remote server), to a remote server. I am going to assume that you have some knowledge of PhoneGap and already have a method, either the Android SDK or the iPhone SDK, to test your native application.
The HTML / JavascriptRather then use a jQuery Ajax call, I am relying on PhoneGap’s FileTransfer object to post the image to a remote server. The code below is all you need to add to your “index.html” PhoneGap file for a basic upload.
Make sure, if you use the example code below, to add your server’s URL in the appropriately marked place.As you can see, the code above allows you to browse for a photo in the device’s photo library using the function getPhoto(). Once a photo is chosen, it gets POSTed as multi-part data to the server.
You will notice a couple of things. 1. I commented out the options.fileName variable. The default name for an uploaded image is “image.jpg.” Also, I’ve included some test parameters in the upload data which will be POSTed to your server as well, but these are optional.
The PHPNow that you are able to find a file and POST it to a server, you’ll need a server side script to handle the data. Here is a very basic PHP example on handling the multi-part form data (image) that was just sent from the device.
The above code uses PHP’s move_uploaded_file() to move the uploaded file from a temporary location to a new directory. To make sure it works, change “/srv/www/upload/” to a directory on your server. I usually have to pass an absolute file-path as the second variable in the move_uploaded_file() for it to work.
You can learn more about PHP uploads in a previous post of mine.FinishedAbove is a short example of how to upload a file to a remote server using PhoneGap, including how to handle that data once it gets to the server.
PHP Send Email Using Authenticated SMTP Mail Server In Real Time
PHP has mail() function to send an email to users.
However this mail() will not work:=>
If sendmail (or compatible binary) is not installed=>
If Apache Web server / Lighttpd running in chrooted jail=>
And your smtp server needs an authentication before sending an email=>
Or you just need to send email using PHP PEARIn all these cases you need to use PHP PEAR's Mail:: interface.
It defines the interface for implementing mailers under the PEAR hierarchy, and provides supporting functions which are useful in multiple mailer backends. In this tip you will learn about how to send an e-mail directly to client smtp server in real time.PHP Pear's Mail.php is located in /usr/share/pear/ directory. Following is sample code to send an email via authenticated smtp server.
PHP send email using PHP SMTP mail Pear functions - Sample source codeFollowing code is well commented, you need to make necessary changes as per your
setup.send($recipients, $headers, $mailmsg);
?>
Sending smtp email from chrooted Apache or Lighttpd webserverRead following section, if you are running a secure chrooted Apache or Lighttpd web server. I have already written about setting php mail() function in chrooted jail.
If you are using chrooted jail server setup, copy all files from /usr/share/pear directory to /chroot-directory/usr/share/pear directory. For example if lighttpd chrooted jail located in /webroot directory, you need to type following commands to install PHP pear support:# mkdir -p /webroot/usr/share/pear# cd /webroot/usr/share/pear# cp -avr /usr/share/pear .
If PHP SAFE MODE is on, you must set /webroot/usr/share/pear directory permission to webserver username to allow access. Otherwise you will see error as follows:1-Nov-2006 09:43:19] PHP Warning: main(): SAFE MODE Restriction in effect. The script whose uid is 506 is not allowed to access /usr/share/pear/PEAR.php owned by uid 0 in /usr/share/pear/Mail.php on line 636.
So if webserver username is lighttpd or apache use following command to setup correct ownership:# chown lighttpd:lighttpd /webroot/usr/share/pear -ROR# chown apache:apache /webroot/usr/share/pear -RYou may also find modified wordpress WP-ContactForm plugin useful. It is a drop in form for users to contact you. It can be implemented on a page or a post.
Original authored by Ryan Duff, which use php mail() function to send email. I have modified the same to send email via my ISP authenticated gateway using PHP PEAR's Mail:: interface :D
However this mail() will not work:=>
If sendmail (or compatible binary) is not installed=>
If Apache Web server / Lighttpd running in chrooted jail=>
And your smtp server needs an authentication before sending an email=>
Or you just need to send email using PHP PEARIn all these cases you need to use PHP PEAR's Mail:: interface.
It defines the interface for implementing mailers under the PEAR hierarchy, and provides supporting functions which are useful in multiple mailer backends. In this tip you will learn about how to send an e-mail directly to client smtp server in real time.PHP Pear's Mail.php is located in /usr/share/pear/ directory. Following is sample code to send an email via authenticated smtp server.
PHP send email using PHP SMTP mail Pear functions - Sample source codeFollowing code is well commented, you need to make necessary changes as per your
setup.send($recipients, $headers, $mailmsg);
?>
Sending smtp email from chrooted Apache or Lighttpd webserverRead following section, if you are running a secure chrooted Apache or Lighttpd web server. I have already written about setting php mail() function in chrooted jail.
If you are using chrooted jail server setup, copy all files from /usr/share/pear directory to /chroot-directory/usr/share/pear directory. For example if lighttpd chrooted jail located in /webroot directory, you need to type following commands to install PHP pear support:# mkdir -p /webroot/usr/share/pear# cd /webroot/usr/share/pear# cp -avr /usr/share/pear .
If PHP SAFE MODE is on, you must set /webroot/usr/share/pear directory permission to webserver username to allow access. Otherwise you will see error as follows:1-Nov-2006 09:43:19] PHP Warning: main(): SAFE MODE Restriction in effect. The script whose uid is 506 is not allowed to access /usr/share/pear/PEAR.php owned by uid 0 in /usr/share/pear/Mail.php on line 636.
So if webserver username is lighttpd or apache use following command to setup correct ownership:# chown lighttpd:lighttpd /webroot/usr/share/pear -ROR# chown apache:apache /webroot/usr/share/pear -RYou may also find modified wordpress WP-ContactForm plugin useful. It is a drop in form for users to contact you. It can be implemented on a page or a post.
Original authored by Ryan Duff, which use php mail() function to send email. I have modified the same to send email via my ISP authenticated gateway using PHP PEAR's Mail:: interface :D
Subscribe to:
Posts (Atom)