Skip to main content

Posts

Showing posts with the label PHP

Basic PHP File Handling — Create, Open, Read, Write, Append, Close, and Delete

I don't do a great deal of file handling in my PHP code -- most of my customers don't have a need for it or there's no room for file creation in the already tight budget. On the rare occasion that I do need to manipulate files, I keep the following tip sheet. Create a File $my_file = 'file.txt' ; $handle = fopen ( $my_file , 'w' ) or die ( 'Cannot open file: ' . $my_file ) ; //implicitly creates file Open a File $my_file = 'file.txt' ; $handle = fopen ( $my_file , 'w' ) or die ( 'Cannot open file: ' . $my_file ) ; //open file for writing ('w','r','a')... Read a File $my_file = 'file.txt' ; $handle = fopen ( $my_file , 'r' ) ; $data = fread ( $handle , filesize ( $my_file ) ) ; Write to a File $my_file = 'file.txt' ; $handle = fopen ( $my_file , 'w' ) or die ( 'Cannot open file: ' . $my_file ) ; $data = 'Th

Sending e-mail from localhost in PHP in Windows Environment

Have you ever been frustrating, why e-mail is not going from the  localhost  while using  XAMPP  or WAMP  or any other  PHP  servers in windows environment? well in that situation i think i can help you.In this article i am  going  to tell you how to send e-mail from  localhost  in PHP . 1)  Open the “ php . ini “. You should know where it is located because it depends upon the particular server you’re running. 2)  Search for the attribute called “ SMTP ” in the  php . ini  file.Generally you can find the line “ SMTP= localhost “. change the  localhost  to the smtp server name of your  ISP . And, there is another attribute called “ smtp_port ” which should be set to  25 .I’ve set the following values in my  php . ini  file. SMTP = smtp.wlink.com.np smtp_port = 25 3)  Restart the  apache  server so that  PHP  modules and attributes will be reloaded. 4)  Now try to send the mail using the  mail()  function , mail(“you@yourdomain.com”,”test subject”,”test body”

Change dropdown list (options) values from database with ajax and php

I’m going to show you a example in php and ajax to change the values of the dropdown’s options without refreshing the page. The values (options) of the dropdown are fetched from the database and the certain portion of the web pages is only refreshed without need to refresh the whole page. Let’s start with creating the two tables country and city and insert some data CREATE TABLE country ( id tinyint(4) NOT NULL auto_increment, country varchar(20) NOT NULL default '', PRIMARY KEY (id) ) TYPE=MyISAM;CREATE TABLE city ( id tinyint(4) NOT NULL auto_increment, city varchar(50) default NULL, countryid tinyint(4) default NULL, PRIMARY KEY (id) ) TYPE=MyISAM; Now let’s look at the html code, let’s look at the code of the form and its elements <form method="post" action="" name="form1"> Country : <select name="country" nChange="getCity('findcity.php?country='+this.value)"> <option

5 useful url rewriting examples using .htaccess

Now let’s look at the examples 1)Rewriting product.php?id=12 to product-12.html It is a simple redirection in which .php extension is hidden from the browser’s address bar and dynamic url (containing “?” character) is converted into a static URL. RewriteEngine on RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1 2) Rewriting product.php?id=12 to product/ipod-nano/12.html SEO expert always suggest to display the main keyword in the URL. In the following URL rewriting technique you can display the name of the product in URL. RewriteEngine on RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ product.php?id=$2 3) Redirecting non www URL to www URL If you type yahoo.com in browser it will be redirected to www.yahoo.com. If you want to do same with your website then put the following code to .htaccess file. What is benefit of this kind of redirection?? Please check the post about  SEO friendly redirect (301) redirect in php and .htaccess. RewriteEngine On Rewrit

Ajax login validation system in PHP using jQuery

I’ve just shown the example without connecting the database and some people have faced problem with database connecting solution of that problem.In this post, I’ll show you how to use Ajax login system in php using jQuery and some animation as well. View Demo In this example, first of all we’ll validate the user login detail from ajax showing the messages with some animation. If authenticated, the user will be redirected to the secure page “secure.php”. If you’ll try to directly access “secure.php”, you can’t do that. Now let’s look at the code how to do this, HTML Code < form method="post" action="" id="login_form" /> < input name="username" type="text" id="username" value="" maxlength="20" /> < input name="password" type="password" id="password" value="" maxlength="20" /> < input name="Submit" type="s