Saturday, October 30, 2010

zend with wamp

Setting up WAMP for Zend Framework Projects
A ZF project would not run out of the box on a fresh installation of WAMP. The purpose of this guide is to act as a checklist for the experienced, and a step by step guide for the more inexperienced. Before we start, this article is written based on WampServer 2.0i with Zend Framework 1.10. If you are using earlier versions, the steps should work, but you might want to consider upgrading to the newest.
1. Make sure Wamp is running ok
2. Enable virtual hosts, mod_rewrite, and override in ‘httpd.conf’
3. Set up virtual host in ‘httpd-vhosts.conf’
4. Check include paths for the Zend library
5. Add Custom URLs in host file
1. Make sure Wamp is running ok
Navigate to http://localhost on your favorite browser, you should see the WampServer welcome screen with headings “Server Configuration”, “Tools”, “Your projects” etc. If not, check your Wamp installation before proceeding further.
2. Enable virtual hosts, mod_rewrite, and override in ‘httpd.conf’
For a default 2.0i installation, the apache configuration file can be found at ‘C:\wamp\bin\apache\Apache2.2.11\conf’. After making a backup, open it in notepad and search for “vhosts”. Make sure the line
# Include conf/extra/httpd-vhosts.conf
is uncommented by removing the “#” in front.
Next, look for the line
# LoadModule rewrite_module modules/mod_rewrite.so
and uncomment it too.
Lastly, look for

Options FollowSymLinks
AllowOverride None
Order deny,allow
# Deny from all

and make sure AllowOverride is set to All instead of none. This prompts Apache to apply rules found in the .htaccess files in sub directories. Save and close.
3. Set up virtual host in ‘httpd-vhosts.conf’
Look for the file in ‘C:\wamp\bin\apache\Apache2.2.11\conf\extra’. Make a copy for backup and open it in notepad. The file should already have 2 dummy entries which you can safely remove. Now we will add the default virtual host and your own custom host:
NameVirtualHost 127.0.0.1:80


ServerAdmin webmaster@localhost
DocumentRoot "c:\wamp\www"
ServerName localhost
ServerAlias localhost
ErrorLog "C:\wamp\logs\apache_error.log"
CustomLog "C:\wamp\logs\access.log" common

Note: replace the parameters below with your own project paths.

ServerAdmin admin@customurl.localhost
DocumentRoot "C:\My\Project\Path\public"
ServerName customurl.localhost
ServerAlias customurl.localhost
ErrorLog "C:\My\Project\Path\logs\apache_error.log"
CustomLog "C:\My\Project\Path\logs\access.log" common

Update: if you get a “You don’t have permission to access” error when accessing the page, include the following directive right before the
tag:

allow from all

4. Check include paths for the Zend library
Your app probably runs alright now. If you are getting the file not found error when including the Zend library, make sure you have copied the Zend library into your /library folder and your index.php actually combines it with the PHP include path:
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path()
)));
Edit: As Fernando pointed out, take note to use slashes “\” instead of back slashes “/” on windows. Also, include your custom urls in your host file if they are not a subdomain of localhost:
5. Add Custom URLs in host file
Your host file is located at ‘C:\Windows\System32\drivers\etc’. Its a file without extension, but you can open it with notepad just the same. Add the following line to the end:
127.0.0.1 [Custom URL here]
That’s it. If you are unsure of the steps or meet any other problems feel free to email me. Tested using WampServer 2.0i with Zend Framework 1.10.

Note : 1- If you are under windows, you have to put the paths with this “/” bar.
For example, in this lines:
DocumentRoot “c:\wamp\www”
ErrorLog “C:\wamp\logs\apache_error.log”
CustomLog “C:\wamp\logs\access.log” common
change it like this:
DocumentRoot “c:/wamp/www”
ErrorLog “C:/wamp/logs/apache_error.log”
CustomLog “C:/wamp/logs/access.log” common
Otherwise, the apache server won’t start.
2- For virtualhosts, apart of changing “httpd-vhosts.conf”, you have to set the hosts file of the OS.
Path -> C:\Windows\System32\drivers\etc\hosts
something like this:
127.0.0.1 customurl # customurl ZF project

Thursday, July 29, 2010

Removing special characters from a string using php

$str = '"!@#$%^&*()MyTest\'_+-|/\/{}[];:<,>.?';
$tempVar = preg_replace('/[^a-zA-Z0-9]*/','',$str);
echo $tempVar;

Wednesday, July 21, 2010

get start and end date according given date and duration

function getEndStartDate($date, $duration) {//date format yyyy-mm-dd
$date_time = strtotime( $date );

$date_start_time = mktime( 0, 0, 0, date('m', $date_time), date('d', $date_time), date( 'Y', $date_time ) );
$date_start = date( 'Y-m-d 00:00:00', $date_start_time );
$date_end = date( 'Y-m-d 23:59:59', strtotime( '+'.$duration.' month -1 day', $date_start_time ) );

return array( 'date_start' => $date_start, 'date_end' => $date_end );}

get start and end date for a week

/**
* This function is used for finding start and end date for a week
*/
function week_from_monday($date) {
// Assuming $date is in format DD-MM-YYYY
list($day, $month, $year) = explode("-", $date);

// Get the weekday of the given date
$wkday = date('l',mktime('0','0','0', $month, $day, $year));

switch($wkday) {
case 'Monday': $numDaysToMon = 0; break;
case 'Tuesday': $numDaysToMon = 1; break;
case 'Wednesday': $numDaysToMon = 2; break;
case 'Thursday': $numDaysToMon = 3; break;
case 'Friday': $numDaysToMon = 4; break;
case 'Saturday': $numDaysToMon = 5; break;
case 'Sunday': $numDaysToMon = 6; break;
}

// Timestamp of the monday for that week
$monday = mktime('0','0','0', $month, $day-$numDaysToMon, $year);

$seconds_in_a_day = 86400;

// Get date for 7 days from Monday (inclusive)
for($i=0; $i<7; $i++)
{
$dates[$i] = date('Y-m-d',$monday+($seconds_in_a_day*$i));
}

return $dates;
}//End week_from_monday

how we can get number of occurance of a value in an array

#array_search_match($needle, $haystack) returns all the keys of the values that match $needle in $haystack
function array_search_all($needle, $haystack){
foreach ($haystack as $k=>$v) {
if($haystack[$k]==$needle){
$array[] = $k;
}
}
return ($array);
}