Monday, June 23, 2008
Getting the current full URL in PHP
{
$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
$protocol = substr(strtolower($_SERVER["SERVER_PROTOCOL"]), 0, strpos(strtolower($_SERVER["SERVER_PROTOCOL"]), "/")) . $s;
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
return $protocol . "://" . $_SERVER['SERVER_NAME'] . $port . $_SERVER['REQUEST_URI'];
}
echo FullURL();
Tuesday, June 3, 2008
Zip Code Distance in PHP or Nearest Zip Codes
http://www.shopno-dinga.com/WebService/zipinfo/zipcodes_2006.txt
and here is php script:
Output display nearest ZIP code first
$zipMain=40011;
$zipOthers=array("15634", "22987", "31044", "40434", "50862", "81224");
foreach($zipOthers as $CurZip)
{
if($CurZip!="")
{
$handle = fopen("zipcodes_2006.txt", "rb");
$contents = '';
while (!feof($handle))
{
$contents=fread($handle, 8192)."<br>";
if(strpos($contents,"\n".$CurZip."||")>0)
{
$contents=substr($contents,strpos($contents,$CurZip),200);
$contents=explode("||",$contents);
$Lat=$contents[1];
$Lon=$contents[2];
$Distance[$CurZip]=precisionDistance($Lat, $Lon, $CurZip);
}
}
fclose($handle);
}
}
asort($Distance);
print_r($Distance); //Display Nearest ZIP code First
function precisionDistance($lat, $lon, $zip = NULL, $precision = 2)
{
$earthsradius = 3963.19;
$pi = pi();
$c = sin($starting_lat/(180/$pi)) * sin($lat/(180/$pi)) +
cos($starting_lat/(180/$pi)) * cos($lat/(180/$pi)) *
cos($lon/(180/$pi) - $starting_lon/(180/$pi));
$distance = $earthsradius * acos($c);
return round($distance,$precision);
}
?>
Tuesday, May 20, 2008
One Page Admin Module / Master Page
What is Admin and Client Viewing? First Client Viewing means the out looking of a site. Like when you write http://www.yahoo.com you get a page. Principle work of Admin panel is the arrangement of this client viewing.
From my short, unstructured, massy and spaghetti development experience i found all web site have number of modules. Almost every module of dynamic site is controlled through admin panel. Whereas every module have 4 basic operations. These are Create, Retrieve, Update and Delete( Called CRUD). Every developer do the same thing in different ways. Database support only these four operation, so no one can get out from this boundary. But the difference between a spaghetti and expert coder is the arrangement/structure of coding to accomplish these operations. Everyone try to manage his code with a good standard. So that anyone can understand and recode / modify / expand code easily after a short or long period of time.
Admin panel is the heart of a dynamic site. When clients are familiar with it they get the facility what they actually wants. For every module basic features are maintained by three/four different pages (I found for most developers). First page is concern with add one item for respective module. Second page display listing of these features. From this page clients can go for edit or delete a specific item. Edit and delete operation are accomplished by two different pages. This was my development strategy last year. Slowly one thinking in my mind arise. Cant i do all the CRUD operations in a single page. It must reduce my time and re-use UI (User Interface). Finally i can make a shape my thinking.
I don't do anything much appreciable. 100 times better then this is already exist in development industry. But this is the implementation of my thinking. That's why it's important for me. And surely it save my time. I use a single page where from i can add, edit, view listing and delete any item of my module. The page is completely customizable and open source. This is the thing what feels to me nice for novice developers. Additionally every module operations are in separate page. All business logic are implemented in this page. It separate logic from user interface. Every module have different logic's for it's operational behavior. This page is also customizable and easy to use and expand. I use here static function feature which is strong and powerful feature of OOP(Object Oriented Programming). You must like it because the features are easy to understand, modify and expand. Here are the benefits of my One Page Admin Module
1. Add, Edit, Listing and Delete all are in same page.
2. All messages (for add, edit, manage or delete) are at the beginning of the page.
3. You can use ordering (if exist for current module).
4. You don't need to change most of the codes.
5. User interface are not within code (so you can easily modify user interface by any IDE)
6. Section wise comment which help you to understand quick.
7. Additional class for
You shall get the complete code here :
http://www.shopno-dinga.com/download/master_page/
Click Download Link, customize and enjoy your coding.
Any advice will be highly appreciated. Really i expect from you.
Registration system in different web sites
Simple registration : Have only few fields like First Name, Last name, Email, Contact Number, User name and Password. After submit you are registration form you are registered member and can enjoy all facility of registered members.
Slightly improved version : uses email verification. They send a secret code to your email. By clicking the verification link you have to confirm you are the owner of this email account. It's a good validation maintained in registration system. Otherwise someone might use your email id and later you can't register by your own.
Membership approved by Admin only. This type of registration contain as usual fields with email verification. First you register and verify your email address. Then admin view your profile (usually through admin panel) and contact with you (via phone, email or any other way). Finally admin approve you as a member. Admin have full right for disapprove/block your membership at any time.
Secured registration : Image verification used at number of sites for registration process. This type of verification use for protect automatic registration (by software).
Paid Registration : Most of the web sites registration is free of cost. but few sites take change for membership. Few site says pay once and get membership forever. And one other category take charge for a period of time.
All these registration are seems same for site visitors. Because of the design and representation of user interface(UI). Functionality are different for different systems, what is almost hidden from users.
Registration is an important part for any web sites. Whatever it's popular like BBC, CNN or new www.novice-developer.com
Optimize MySq Database
For any dynamic site data is the most valuable thing. Without data dynamic site is valueless. Day by day data are added into your server. And after a certain period of time (depending on the amount of data you insert) there are huge amount of data into your server. Moreover, dynamic sites drive in such a manner that, data are fetched(read) from database and display in site.
Now the question is when there will be a large amount of data in your web site, isn't it take much time to read data from the database and makes your site slow to slower automatically. The answer is yes. No doubt : your site will be slower as data inserted into your system. Now the question arise : OPTIMIZATION.
Optimization have a vital role in performance issue. Now this is one possible way to optimize your database (mysql). Use this line of code:
optimize table TableName;
analyze table TableName;
ALTER TABLE ` TableName` ADD INDEX ( `fieldID` ) ;
Or
ALTER TABLE ` TableName` ADD INDEX ( ` fieldID1` , ` fieldID2` ) ;
I think it will be helpful for you. Let me know is it helpful enough?
Increase file upload size in PHP
There are two considerable factors for upload any file. These are
1. Execution time
3. File Size.
Your script must need to execute until the file is uploaded completely. And offcourse your server alow your expected upload file size. Php have default configuration for both issue. Execution time is set by default 60 seconds. And allowed maximum file size is up to 2MB. Now what shall you do for a audio or video file. Your internet connection might slow and need more execution time then default. Again you may need more file size to be allowed then 2MB. The question is how??
To increase your execution time write a single line of code at the beginning of your script. here it is:
set_time_limit(0);
?>
Now the big issue. Have to increase maximum file size as you need. One solution like:
ini_set("upload_max_filesize",30);
?>
For view current maximum allowed size (before and after change) use echo like:
echo ini_get("upload_max_filesize");
?>
Now the most critical one. Though almost all server allow set_time_limit(0) but Most of the server doesn't allow ini_set("upload_max_filesize",30). I find one more alternatives. You can edit the maximum size by using .htaccess. Means create a file named .htaccess write the following code in that file. Now upload it at the same path where your source file is. Now run your script and test with echo ini_get("upload_max_filesize");
php_value upload_max_filesize "25M"
Enjoy your coding....... If you get any more solution please let me know. I am most novice one so i don't know much but like to learn.
Send Mail From Local Machine (Configuration)
Many developer today like to test php mail script from localhost. Dislike to upload script in server for test. Moreover, like to finalize everything from localhost/own development platform. You must need an internet connection. Furthermore need to configure your php.ini file what send mail using local SMTP.
A number of php.ini files are there in PHP, MySql and Apache. Which one you have to configure? Definitely not everyone. Mail as well as SMTP are very closely and intimately related with apache (web server). So you have to configure php.ini under apache\bin\php.ini. Just follow the following instructions: Change the configuration of php.ini file accordingly:Default: ;SMTP = localhost Change To : SMTP = "mail.gmail.com" Effects/Reason for Change: Give outgoing mail address
Default: max_execution_time = 30 Change To : max_execution_time = 90 Effects/Reason for Change: Sometimes this execution takes time more then 30 sec.
Now use mail() function to send an Email from localhost.
Example:-
$MailTo="novice.devlpr@gmail.com";
$MailFrom="
$MailFromEMail="novice.devlpr@gmail.com";
$Subject="Test mail from Localhost";
$MailBody="Message: Testing Mail from Localhost You can write here .";
$Headers = 'MIME-Version: 1.0' . "\r\n";
$Headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$Headers .= "From:".$MailFrom."<".$MailFromEMail.">\r\n";
echo $MailBody;
if(mail($MailTo,$Subject,$MailBody,$Headers))
echo "Mail sent Successfully.";
else
die("Sorry! The mail not sent successfully.");