You are currently browsing the yalamber.com posts tagged: Tutorials


Storing your session to database in php

I am developing a custom solutions for my projects. I was finding solution to secure my php applications. As security is top concerned when I develop my applications. I had to develop a solution for a shared hosting environment and found out that the session hijacking was a big problem while on shared server. The best solution was to store the session in some where secured place. I Researched about it and fund out that there was already a solution in php to do it. we could actually use session_set_save_handler() function to do it. And for the location to store session database was the suitable place. As database has it’s own authentication layer. So i decided to use it for my applications.

First create a databse table with following sql in you application datatbase:


CREATE TABLE IF NOT EXISTS `yourAppPrefix_sessions` (
`id` varchar(32) NOT NULL,
`access` int(10) unsigned default NULL,
`data` text,
PRIMARY KEY  (`id`)
) ENGINE=MyISAM;

Below i wil provide you the code to use it on your own application:


session_set_save_handler('_open',
'_close',
'_read',
'_write',
'_destroy',
'_clean');

function _open()
{
return true;
}

function _close()
{

return true;
}

function _read($id)
{

$id = mysql_real_escape_string($id);

$sql = "SELECT data
FROM   `[p]sessions`
WHERE  id = '$id'";

if ($result = mysql_query($sql))
{
if (mysql_num_rows($result))
{
$record = mysql_fetch_array($result);

return $record['data'];
}
}

return '';
}

function _write($id, $data)
{

$access = time();

$id = mysql_real_escape_string($id);
$access = mysql_real_escape_string($access);
$data = mysql_real_escape_string($data);

$sql = "REPLACE
INTO    `[p]sessions`
VALUES  ('$id', '$access', '$data')";

return mysql_query($sql);
}

function _destroy($id)
{

$id = mysql_real_escape_string($id);

$sql = "DELETE
FROM   `[p]sessions`
WHERE id = '$id'";

return mysql_query($sql);
}

function _clean($max)
{

$old = time() - $max;
$old = mysql_real_escape_string($old);

$sql = "DELETE
FROM   `[p]sessions`
WHERE  access < '$old'";

return mysql_query($sql);
}

Save the above code and name it session.php or anything you want.

After this where you want to use session in your application. just add following to start session:


//Store session to database
require_once("path_to/session.php");
session_start();

Also be sure you have already connected your database before the above code in your applicaiton as it uses database to store session.

Now after this is done you can use session as usual. This just changes the place where your session is stored i.e in database. Other things are as same as you would store session in the local machine. I hope it will make you clear about what I am talking about.

Best tutorials site for web development

Today I am going to post some of the best available tutorial sites for web development and designing.

1. www.tizag.com

I find this site very useful for starter. It has tutorials on html, css, javascript, php, asp, xml, mysql etc. I refer this site if you are beginner and want to get some fundamentals on any of the language above.

2. www.w3schools.com

This site provides tutorials on most of the web languages. It also has some quiz to test your skills. Get in to this school before you get ready for web arena.

[ad#yalamber]

3. www.tutorialized.com

Offers wide variety of tutorials categorized in various category. It actually doesn’t provide it’s own tutorials. What it actually does is provide links to various site that are providing tutorials. The links are opened in the iframe within the site. It will be very useful if you once follow the sites I listed above and then use this site to get some practice.

These should be enough. If you want to add any please comment. It will be useful.

Tcpdf a pdf generating php class

I needed to generate a pdf dynamically from the website for one of my project. On my search for a good library i came across TCPDF . It is a Free Libre Open Source Software (FLOSS). You can see more about it here. What today i am going to write is to how quickly use tcpdf for your pdf generation need on the fly.

First download tcpdf and extract it.

Upload the extracted tcpdf folder to your site.

now set the variable according to your site in tcpdf/config/tcpdf_config.php file.

Now after you have successfully installed tcpdf. Now you can see examples to generate the pdf in examples folder or follow following steps. I had to generate pdf from html text so i did following.

Create a new php file Let’s call it pdf.php

In that file include the required library files.


require_once('../../tcpdf/config/lang/eng.php');
require_once('../../tcpdf/tcpdf.php');

After that create a new pdf instance


// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true);

After that set some pdf properties


// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor("Your name here");
$pdf->SetTitle("Give your pdf a title");
$pdf->SetSubject("set a subject here);
$pdf->SetKeywords("put, keywords, here, seperating, with, commas");

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);

Below are some settings for header footer fonts, page breaks, etc


// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

//set some language-dependent strings
$pdf->setLanguageArray($l);

This will initialize your document


//initialize document
$pdf->AliasNbPages();

Adds a page in your pdf document

// add a page
$pdf->AddPage();

Set a desired font here

// set font
$pdf->SetFont("vera", "", 9);

This is the variable containing html content.

$htmlcontent = "Put your html conent here";

This will write html content to your pdf document

// output the HTML content
$pdf->writeHTML($htmlcontent, true, 0, true, 0);

// reset pointer to the last page
$pdf->lastPage();

Finally output the pdf document.

//Close and output PDF document
$pdf->Output();

After you call this page from your web browser. you will get a pdf file with the content as described.

here is full source code


<?php
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor("Your name here");
$pdf->SetTitle("Give your pdf a title");
$pdf->SetSubject("set a subject here);
$pdf->SetKeywords("put, keywords, here, seperating, with, commas");

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

//set some language-dependent strings
$pdf->setLanguageArray($l);

//initialize document
$pdf->AliasNbPages();

// add a page
$pdf->AddPage();

// ---------------------------------------------------------

// set font
$pdf->SetFont("vera", "", 9);

// create some HTML content
$htmlcontent = "put your html content here";

// output the HTML content
$pdf->writeHTML($htmlcontent, true, 0, true, 0);

// reset pointer to the last page
$pdf->lastPage();

// ---------------------------------------------------------

//Close and output PDF document
$pdf->Output();

//============================================================+
// END OF FILE
//============================================================+
?>

Send sms to meromobile for free script

YOu might have seen some websites offering sms to meromobile for free. Ever windered how they did it. Ever wanted to use it in your website but didn’t knew how to, here is a quick tutorial for you to get started. Before you start below are some reuirements to meet.

Requirements:

Server with php support and mail function enabled.

ok now let’s get started. The website offering free sms to meromobile are actually?using the service sms to email from meromobile. Sms to email service is actually a service provided by meromobile to send and recieve email by sms. You can send email to any email address using the sms to email service and recieve sms by a email. For ex if you have a meromobile number your mobile email address will be 9779804301307@sms.spicenepal.com?so when anybody sends you email at 9779804301307@sms.spicenepal.com then you will get sms from meromobile with the message.

So how to use this service to offer free sms to meromobile in your site. What can be done is create a form in your website and send?email to the desired mobile no email address. OK here is a way to do it. We?can use any form to mail script available for this purpose.

First of all create a form with 4 fields, name, number, message, email. then?you will need a php processor to process the form and send the mail to the 977(mobile number entered)@sms.spicenepal.com.

Download the codes? Sms to mero here


Tags