PHPCamp 2010 – What/When/Why/Where?

What is PHPCamp?
 
PHPCamp is a ‘ad-hoc gathering’ for PHP community. It similar to barcamp, but more focused towards PHP based web application development.
It is targeting all the PHP enthusiast and starters to join this technical extravaganza this means that anyone can come to PHPCamp and participate.
  
Why attend PHPCamp?
 
Navin has the best answer: “Because learning from humans is much better than Google
  
But I am a student and I have classes…
 
Here is why you should bunk classes to attend PHPCamp / BarcampPune: projects, internships, recosBarcamp?
  
What to expect at PHPCamp?
 
Knowledge, Knowledge and Knowledge. Oh! did I miss Free T-Shirt, Adobe® Flex™ Builder 3 Pro (Licensed Copy) along with “Getting started resources” ( CD also includes lot of “Raffles” ) and of course you could be an “Expert” in 8 hours.
  
When and Where?
 
Venue: SCDL(Symbiosis Center for Distance Learning), Pune
Time: 9AM – 5PM. ( Please reach 15 mins prior )
Landmark: 3-5 mins walk from OM Super Market(Groceries Shop) / SICSR College
  
Register ASAP: http://phpcamp.eventbrite.com/ The number of allowed participants are limited, we have already increased the number twice.
  
Event Website: http://phpcamp.org
  

Password protect your directory without .htaccess

.htaccess is generally used for basic authentication of a folder/website. The data of users logging in using .htaccess is not stored or kept. It works untill the user’s browser remembers the credentials.  And it would really be very difficult to keep track of users logging in using this method.

So, here is a very simple way of doing the same using HTTP authentication:

<?php
$myusername
= “amanjain.com”;
$mypassword = “amanjain”;

/*The authentication technique can be different like using database or a key value pair file*/
if (
$_SERVER["PHP_AUTH_USER"] == “” || $_SERVER["PHP_AUTH_PW"] == “” ||$_SERVER["PHP_AUTH_USER"] != $myusername || $_SERVER["PHP_AUTH_PW"] != $mypassword)
{

header(“HTTP/1.0 401 Unauthorized”);
header(“WWW-Authenticate: Basic realm=\”This is my protected area\”"); /*Replace  ”This is my protected area” with your personal message*/
echo
“<h1>Authorization Required For Accessing Content.</h1>”;/*If user cancels the authentication*/
die();

}

?>

That’s it. Simple right?

Check out PHPCamp.net a article sharing website relevant to our own PHP community

Taking screenshot of a website using PHP on windows os

I have been trying to figure out a way using I could take a screenshot of any provided URL using PHP.
Finally I could find out a way of doing it and that is by using the COM object on windows os.

I have written a class for taking the screenshot.
For using it, all you need to do is:

Download the class file from here.
Rename it and remove the .txt entention
Include the class file in the required script.
And then,
Create a new object:

$s = new akjScreenshot(”http://amanjain.com”);

Resize if required:

$s->toWidth(100);
$s->toHeight(100);

Save:

$s->save(”screen.jpg”);

You can also save it in different formats like:

$s->save(”screen.png”, “png”);
$s->save(”screen.gif”, “gif”);

I hope it helps :) Please leave a comment if you find it useful.

Check out PHPCamp.net a article sharing website relevant to our own PHP community

Making mail() function of PHP work under windows os

mail() is a useful function in PHP for sending mails, but many developers face problems in making mail() work properly on Windows.


In the section [mail function] in the php.ini file, you’ll find three settings:
SMTP, sendmail_from, and sendmail_path.


If your server runs on a Windows machine, you’ll have to set the SMTP option to point to your SMTP server for sending mail using PHP.


So, we must understand that PHP must have the address of some SMTP server where it would deliver the mail to be sent and that is all PHP can actually do.


So, we need  to set SMTP to the SMTP server and the sendmail_from option to the required email address.
Now if we want to use localhost as the SMTP server, we must have some SMTP server running on our system, simple isn’t it?  So we can download and install hMailServer from www.hmailserver.com/ for serving our purpose.


After installing the mail server make sure that you switch off  SMTP authentication for connections coming from localhost / 127.0.0.1 in hMailServer for your mail() function to work.


And this is how [mail function] part of your php.ini should look:


[mail function]
; Setup for Windows systems
SMTP = localhost
sendmail_from = me@mydomain.tld

Height Adjustment of Multicoulmn Page Layout

There are times when we need to make a multi-column layout for our webpage with different background colors. One way is to set the float property of style attribute accordingly:



<div>
<div style=”float:left;background:#c1ece0;width:380px”>
…someText
</div>
<div style=”float:left;background:#63ecc7;width:380px”>
…otherText
</div>
</div>


but this results in something like below :

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc suscipit, ligula et cursus dignissim, mi tellus suscipit felis, quis fringilla lorem nisl non orci. Integer euismod nisl at velit ultricies rhoncus volutpat arcu mollis. Ut pharetra facilisis lacinia. Phasellus justo massa, bibendum ac gravida non, tempor vel felis. Etiam ac libero ut orci lacinia volutpat. Quisque quis nunc vel orci auctor fringilla. Aenean aliquam orci non mi accumsan ultricies. Cras vestibulum venenatis purus sed pretium. Nam erat sapien, pharetra ac rutrum mollis, cursus quis dui. Sed est turpis, rhoncus at auctor vitae, fermentum nec erat. Suspendisse potenti.


Aenean diam arcu, sodales a dapibus sed, tristique non nulla. Etiam quis felis eu purus malesuada elementum. Integer ac metus justo. Morbi iaculis erat id nisl ultrices iaculis. Aenean leo risus, congue eget tincidunt eget, tincidunt vel leo. Pellentesque malesuada lacus fermentum est euismod a cursus sem scelerisque. Nunc sodales luctus accumsan. Sed iaculis egestas vestibulum. Pellentesque vehicula purus ac nunc ullamcorper id cursus neque posuere. Maecenas tincidunt, libero eu tincidunt eleifend, turpis tellus auctor dolor, nec feugiat elit diam sed purus. Proin augue nunc, pretium sit amet sodales faucibus, feugiat et ligula. Aliquam ut ipsum arcu, in lacinia neque. Aliquam id enim vitae nisi viverra pharetra tempus viverra nisl. Integer sit amet egestas sem. Curabitur tempus, ante quis bibendum pretium, enim neque placerat justo, ac posuere nisi odio non mi. Donec fringilla massa in turpis commodo a fringilla erat ornare. Phasellus aliquet tellus vitae risus viverra convallis. Nam convallis sagittis nibh.
In publishing and graphic design, lorem ipsum is the name given to commonly used placeholder text (filler text) to demonstrate the graphic elements of a document or visual presentation, such as font, typography, and layout. The lorem ipsum text, which is typically a nonsensical list of semi-Latin words, is a hacked version of a Latin text by Cicero, with words/letters omitted and others inserted, but not proper Latin.
As nonsense (not actual Latin), the text is not italicized. The text often begins as, “Lorem ipsum dolor sit amet, consectetur adipisicing elit”. Substituting this dummy text for meaningful content should not be confused with “greeking”.
Even though using “lorem ipsum” often arouses curiosity because of its resemblance to classical Latin, it is not intended to have meaning. Where text is visible in a document, people tend to focus on the textual content rather than upon overall presentation, so publishers use lorem ipsum when displaying a typeface or design elements and page layout in order to direct the focus to the publication style and not the meaning of the text.




Now as we can see that the heights of the columns are unequal and therefore the page looks dirty. What we can do is:


1. Put the whole thing in a div, and set overflow:hidden
2. Set margin-bottom: -3500px; padding-bottom: 3500px; i.e. to an extent which cannot be the difference in height of the column.


<div style=”overflow:hidden”>
<div style=”float:left;background:#c1ece0;width:380px;margin-bottom: -3500px; padding-bottom: 3500px;”>
…someText
</div>
<div style=”float:left;background:#63ecc7;width:380px;margin-bottom: -3500px; padding-bottom: 3500px;”>
…otherText
</div>
</div>



Output:


Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc suscipit, ligula et cursus dignissim, mi tellus suscipit felis, quis fringilla lorem nisl non orci. Integer euismod nisl at velit ultricies rhoncus volutpat arcu mollis. Ut pharetra facilisis lacinia. Phasellus justo massa, bibendum ac gravida non, tempor vel felis. Etiam ac libero ut orci lacinia volutpat. Quisque quis nunc vel orci auctor fringilla. Aenean aliquam orci non mi accumsan ultricies. Cras vestibulum venenatis purus sed pretium. Nam erat sapien, pharetra ac rutrum mollis, cursus quis dui. Sed est turpis, rhoncus at auctor vitae, fermentum nec erat. Suspendisse potenti.


Aenean diam arcu, sodales a dapibus sed, tristique non nulla. Etiam quis felis eu purus malesuada elementum. Integer ac metus justo. Morbi iaculis erat id nisl ultrices iaculis. Aenean leo risus, congue eget tincidunt eget, tincidunt vel leo. Pellentesque malesuada lacus fermentum est euismod a cursus sem scelerisque. Nunc sodales luctus accumsan. Sed iaculis egestas vestibulum. Pellentesque vehicula purus ac nunc ullamcorper id cursus neque posuere. Maecenas tincidunt, libero eu tincidunt eleifend, turpis tellus auctor dolor, nec feugiat elit diam sed purus. Proin augue nunc, pretium sit amet sodales faucibus, feugiat et ligula. Aliquam ut ipsum arcu, in lacinia neque. Aliquam id enim vitae nisi viverra pharetra tempus viverra nisl. Integer sit amet egestas sem. Curabitur tempus, ante quis bibendum pretium, enim neque placerat justo, ac posuere nisi odio non mi. Donec fringilla massa in turpis commodo a fringilla erat ornare. Phasellus aliquet tellus vitae risus viverra convallis. Nam convallis sagittis nibh.
In publishing and graphic design, lorem ipsum is the name given to commonly used placeholder text (filler text) to demonstrate the graphic elements of a document or visual presentation, such as font, typography, and layout. The lorem ipsum text, which is typically a nonsensical list of semi-Latin words, is a hacked version of a Latin text by Cicero, with words/letters omitted and others inserted, but not proper Latin.
As nonsense (not actual Latin), the text is not italicized. The text often begins as, “Lorem ipsum dolor sit amet, consectetur adipisicing elit”. Substituting this dummy text for meaningful content should not be confused with “greeking”.
Even though using “lorem ipsum” often arouses curiosity because of its resemblance to classical Latin, it is not intended to have meaning. Where text is visible in a document, people tend to focus on the textual content rather than upon overall presentation, so publishers use lorem ipsum when displaying a typeface or design elements and page layout in order to direct the focus to the publication style and not the meaning of the text.

gnUnify 2009 – Ajax with PHP

I gave a talk on ajax at gnunify’09. This is the slide I used to give the talk.

View more presentations from jainaman.

gnUnify – 2009 on 13th & 14th Feb at SICSR

Create database backup & email using php

It is generally difficult to backup our database regularly and store them. With more and more storage space being provided by our email providers, we can write a script that can be called when required and it will automatically create database backup and mail it to the specified email.

The following zip contains two files:
1. ajDbBackup.class
2. backup.php

ajDbBackup.class file contains the ajDbBackup class which can be used to backup the database. An example of usage is given in file backup.php .
To email backup create a php file in the same directory where ajDbBackup.class is kept with the following data:
<?php
include ‘./ajDbBackup.class’;
$backup = new ajDbBackup;
$backup->emailTo = “email@domain.tld”;
$backup->emailFrom = “emailTwo@domain.tld”;
$backup->host = “mysqlHost”;
$backup->user = “mysqlUser”;
$backup->password = “mysqlUserPassword”;
$backup->dbName = “mysqlDbNameToBeBacked”;
$backup->ajMail();
?>

Give execution and read/write permissions to the files. Just call the file you have created through any web browser.

Skipping pass phrase requirement while restarting a server

There are a lot of times we need to write automated script which can start/restart a server when ever required. Or generally if we are restarting the server quite a lot of time (maybe for testing purpose), we are prompted to enter pass phrase if we have installed  libapache-mod-ssl and have configured with any one of the domains on the server.

So, sometimes we feel the need of skipping the pass phrase prompt. Here is what we need to do:
1. Remove the encryption from the RSA private key (we will also preserve the original file):
$ cp server.key serverOld.key
$ openssl rsa -in serverOld.key -out server.key
2. We should make sure that server.key is only readable by root by:
$ chmod 400 server.key

After this we won’t be prompted for pass phrase every time we restart the server.

Using BSNL EVDO modem on Ubuntu 8.10

We will use wvdial for using EVDO. Wvdial dialer is PPP dialer, which dials a modem and starts PPP to connect to the Internet.

Steps to connect are:
1. As root, in terminal, type :
# wvdialconf /etc/wvdial.conf
2. Now edit /etc/wvdial.conf, type:
# vim /etc/wvdial.conf
2. Now enter your username, password, & phone no as:
Phone = #777
Username = cdma
Password = cdma

3.Check your wvdial.conf file for these two entries:
Stupid Mode = 1
Auto DNS = 1
If they are not there, add them.
4. Save the file wvdial.conf and exit the text editor.
5. Now type sudo wvdial in the terminal and the EVDO should start working :)

FOR REFERENCE:
My /etc/wvdial.conf contains:

[Dialer Defaults]
Modem = /dev/ttyUSB1
Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Stupid Mode = 1
Modem Type = Analog Modem
Phone = #777
New PPPD = yes
ISDN = 0
Username = cdma
Password = cdma
Baud = 9600

<< Previous Entries