| 2 comments ]


function get_url(){

$server = empty( $_SERVER["HTTPS"] ) ? '' : ( $_SERVER["HTTPS"] == "on" ) ? "s" : "";

$root= substr( strtolower ( $_SERVER["SERVER_PROTOCOL"] ), 0,
strpos( strtolower( $_SERVER["SERVER_PROTOCOL"] ), "/") ) . $server;

$root .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$root.= $_SERVER["SERVER_NAME"]. " : " .$_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
} else {
$root.= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
}
return $root;

}

| 0 comments ]

The following function will help you to create a thumbnail for an image.

function get_thumb($img_filename, $maxwidth, $maxheight) {

$img_size = getimagesize($img_filename) ;

if($img_size) {

$width = $img_size [0];

$height = $img_size [1];

$thumb_height=$ height;

$thumb_width=$width;

if ($width > $maxwidth) {

$thumb_width = $maxwidth;

$thumb_height = floor($height * ($maxwidth / $width));

}

if ($thumb_height > $maxheight) {

$thumb_width = floor($thumb_width * ($maxheight / $thumb_height));

$thumb_height = $maxheight;

}

if ( (imgsizearray['mime']== 'image/jpeg')|| (imgsizearray['mime']== 'image/png')|| (imgsizearray['mime']== 'image/gif')) {

$type=explode('/', $imgsizearray['mime']);

$thumb_image = imagecreatetruecolor($thumb_width, $thumb_height);

$image ="imagecreatefrom" . $type[1] ($img_filename);

if($image){


/*Copy and resize part of an image with resampling*/

imagecopyresampled($thumb_image, $image, 0,0,0,0,$thumb_width,$thumb_height,$width,$height);

}

imagejpeg($thumb_image, null, 100);

}

}

return false;

}

you can call the function like this . get_thumb ($file,100,100);

$img_filename is the uploaded file,and $maxwidth and $maxheight are the height and width specification for thumb

| 1 comments ]

CAPTCHAs are used to prevent automated software from performing actions.CAPTCHAs have several applications for practical security, including Preventing Comment Spam in Blogs, Preventing Comment Spam in Blogs,CAPTCHAs also offer a plausible solution against email worms and spam.

The following is an example for securing a form with captcha

main.php

<?php

require_once('functions.php');

If($_POST){

If(validate_captcha()){

echo "captcha okay";

//do your actions here

else{

echo " captcha not okay";

}

}

}else{

mt_srand ((double)microtime()*1000000);

$maxran = 1000000;

$random = mt_rand(0, $maxran);

$html = "<html>

<header><title>form with captcha</title></header>

<body> <form name='frm' action='' method='post'>";

$html.="name:<input type='text' name='txtname'>";

$html.="password:<input type='password' name='txtpwd'>";

$html .= "<div id=\"Registercode\"><table><tr><td><img src='". create_captchaImg.php?ran=".$random." ></td> ";

$html .= "<td><input type=\"text\" name=\"captchacode\" maxlength=\"10\" /></td></tr></table></div>";

$html .= "<input type='hidden' name='randomcode' value='". $random."'>

<input type='submit' name='submitbtn' value='submit' >

</form>

";

echo $html;

}

?>

functions.php

<?php

/* function to generate captcha code */

function generate_captcha($num) {

$timemd5=md5($num);

$date = date('F j');

$year=date('Y');

$temp=md5($num . $date.$year . $timemd5);

$capcode = hexdec($temp);

$code = substr($capcode, 4, 6);

return $code;

}

/* function to validate captcha*/

function validate_captcha() {

$captchacode = $_POST['captchacode'];

$randomcode = $_POST['randomcode'];

$generated_code = generate_captcha($randomcode);

$valid = false;

if ((trim($captchacode) != "") && (strcmp($captchacode, $generated_code) == 0))

$valid = true;

else

$valid=false;

return $valid;

}

?>

create_captchaImg.php

<?php

/* function to generate captcha image with the captcha code.choose some image as background */

require_once('functions.php');

$ccode=$_REQUEST['ran'];

$capcode = $ccode;

$code = generate_captcha($capcode);

$image = ImageCreateFromJPEG("images/captchabg.jpeg");//give captcha background image path

$text_color = ImageColorAllocate($image, 80, 80, 80);

Header("Content-type: image/jpeg");

ImageString ($image, 5, 12, 2, $code, $text_color);

ImageJPEG($image, '', 75);

ImageDestroy($image);

}

?>

| 2 comments ]

Function to find out last 12 months details from the current date

function getLast12MonthsDetails(){

        $month = date('m');
        $year  = date('Y');
        $i = 1;
        $date = a rray();
        while($i<=12){
          $timestamp = mktime(0,0,0,$month,1,$year);
          $date[$i]['month']      = date('F', $timestamp);
          $date[$i]['monthCount'] = date('m', $timestampts);
          $date[$i]['monthShort'] = date('M', $timestamp);
          $date[$i]['daysInMonth'] = date('t', $timestamp);
          $date[$i]['year']      = date('Y', $timestamp);
          $date[$i]['yearShort']  = date('y', $timestamp);
          $month--;
          $i++;
        }
        return $date;
    }


calling the function

print_r(getLast12MonthsDetails());

will return the following details in an array

Array
(
[1] => Array
(
[month] => July
[monthCount] => 12
[monthShort] => Jul
[daysInMonth] => 31
[year] => 2009
[yearShort] => 09
)

[2] => Array
(
[month] => June
[monthCount] => 12
[monthShort] => Jun
[daysInMonth] => 30
[year] => 2009
[yearShort] => 09
)

[3] => Array
(
[month] => May
[monthCount] => 12
[monthShort] => May
[daysInMonth] => 31
[year] => 2009
[yearShort] => 09
)

[4] => Array
(
[month] => April
[monthCount] => 12
[monthShort] => Apr
[daysInMonth] => 30
[year] => 2009
[yearShort] => 09
)

[5] => Array
(
[month] => March
[monthCount] => 12
[monthShort] => Mar
[daysInMonth] => 31
[year] => 2009
[yearShort] => 09
)

[6] => Array
(
[month] => February
[monthCount] => 12
[monthShort] => Feb
[daysInMonth] => 28
[year] => 2009
[yearShort] => 09
)

[7] => Array
(
[month] => January
[monthCount] => 12
[monthShort] => Jan
[daysInMonth] => 31
[year] => 2009
[yearShort] => 09
)

[8] => Array
(
[month] => December
[monthCount] => 12
[monthShort] => Dec
[daysInMonth] => 31
[year] => 2008
[yearShort] => 08
)

[9] => Array
(
[month] => November
[monthCount] => 12
[monthShort] => Nov
[daysInMonth] => 30
[year] => 2008
[yearShort] => 08
)

[10] => Array
(
[month] => October
[monthCount] => 12
[monthShort] => Oct
[daysInMonth] => 31
[year] => 2008
[yearShort] => 08
)

[11] => Array
(
[month] => September
[monthCount] => 12
[monthShort] => Sep
[daysInMonth] => 30
[year] => 2008
[yearShort] => 08
)

[12] => Array
(
[month] => August
[monthCount] => 12
[monthShort] => Aug
[daysInMonth] => 31
[year] => 2008
[yearShort] => 08
)

)

From the above array, we can get all the details of last 12 months.
hope that this will helpful for you guys.

happy PHPing

I wish, you guys, please comment about this post in following box.

| 0 comments ]

The river is an activity stream containing descriptions of activities. In new elgg there is a plugin called “riverdashboard” , And you can add river for your plugins too.Each river may be added after an activity such as add delete etc .You can use the following function for doing this.

add_to_river($viewname,$actionname,$userguid,$entityguid);

The first parameter $viewname is the name of the view that will display the river item and the next one $actionname is the action and the other two parameters are the guid of the user performing the action, and the guid of the entity being acted on.

Elgg recommends the view location of the river views as follows /river/classname/event, where classname is the class you’re interested in (ElggObject for objects, ElggUser for users, and so on) and event is the event (create, update, delete and so on).

River item information will be passed in an object called $vars['item'], which contains two important parameters $vars['item']->subject_guid that is the user guid and the action guid that is the $vars['item']->object_guid .

| 2 comments ]

I would like to share some basics about plugin development in elgg .

In elgg all the plugins will be in mod directory. You can drop your plugin in this directory.

First you need a name for your plugin.The names of the plugins in an installation must be unique.


you need a ‘start.php’ file for each plugin which to describe the common functions for a plugin.This file can be considered as the core controller of all plugins .The start.php must have an init function which initializes the plugin .

You have to register this function to elgg system and have to make sure the initialisation function is called on initialisation.

The following is a sample if the init functuion is yourplugin_init()

register_elgg_event_handler('init','system',’yourplugin _init');


In common the elgg plugin will have following folders

actions : Commonly all action(normally form actions) files are saved in this folder.

And you can register all the actions involved in a plugin with in its start.php file

The function is as follows .

register_action($action, $public = false, $filename = "", $admin_only = false)

For example

register_action('blog/add',false,$CONFIG->pluginspath . "blog/actions/add.php");

the parameters of the register_action indicates

action : The name of the action (here "blog/add")

second parameter is a Boolean parameter.which indicates whether the action is public or not that is whether this action be accessed by people not logged into the system or not.In above example false indicates that it is not a public action.

Next is filename that is where the action is located here the action is located in blog/actions/add.php , $CONFIG->pluginspath gives the path to plugin s that is the full path to mod directory of your elgg installation .

And if it is required you can register the action as admin action .you have to set the fourth parameter as true for this by default it in false.


Views : In common all html files will be in this folder .You can call these views in the plugin using the function

elgg_view(‘viewname’,$array_of params)

You can override existing views also.The view lodes based on the plugin priority.

You can extend views using the function extend_view('original view ','your view');


The view folder contains a default folder which holds all the views of plugin .For the easiness .For the easiness you can use subfolders in the default folder.

For example if you have a plugin named blog and it have a view called create.php in the path views /defaults/blogs/create.php you can call this view like ‘blogs/create ‘ .That is you can call the view as follows elgg_view(‘blogs/create’)

Language file :

This file contains the language translations for the plugin.

Creating Friendly URLS:

You can create nice URLs for your plugin using page handler function.for doing this you have to define and register a page handler in `start.php`,


function handlerfunction($page) {

switch ($page[0])

{

case 'index':

Your code for the index view

break;

case 'your_view':

include(‘yourview’);

break;

}

}

register_page_handler(handler, 'handler_function’);

in this function the first argument handler is your entity type, the next argument is the your plugin function handling the request which will get passed as an array in `$page` this carries each url segments. You can check for a location and can create the cases corresponding to that.



User settings:

Plugins might have some settings , You can create the view in the file settings.phpand you can retrieve the settings by the following function

get_plugin_setting($name, $plugin_name );

and you can set values using

set_plugin_setting($name, $value, $plugin_name);function

You can create widgets on your plugins .The widget might be in the folder views/default/widgets

The widgets folder may have two files one is view.php that is view of widget and the other is eidt.php that is the page for setting the widget display options like number of items to be displayed etc.

You can initialize the widget in the init function using the following function

add_widget_type('widgettitle',$name_of_widget,$desc_of_widget) .

| 1 comments ]

Hello ,

I am Bibin John, fromKerala. I have seen you in a facebook group 'Php Developers in KERALA'
This mail is regarding an issue ,that i am facing with php. I have a php website In this site there is a feedback form, when I submit that form It shows an error like this,:
Parse error: syntax error, unexpected T_STRING in /home/tecwand/public_html/contact.php on line 1

I am using cpanel accelerated hosting , PHP5.2.8. I think the problem with this version. My friend using 5.2.6 in cpanel, but no problem with the same PHP script that I used!
Below I mentioned the PHP script I used , Can you make its syntax suitable for php 5.2.8 ?

<?php
$name=$_REQUEST['name'];
$email=$_POST['email'];
$comment=$_POST['comment'];
$con=mysql_connect("localhost","root","");
mysql_select_db("tecwand",$con);
$ins="insert into comments values('$name','$email','$comment')";
mysql_query($ins);
echo"inserted";
?>
Hope that you will help me!
Kindly make replay to this e mail address!

Thanks and Regards,
Bibin John