Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Friday, November 11, 2011

15 Tips and tricks for php

PHP is used in more a less most websites on the internet. I have compiled a list of 15 top useful PHP tips / tricks and tutorials. These should keep you busy for a while. They all can come in handy sometime when you are coding.

Limit Characters From Your Text

In this tutorial you will learn how to limit characters from a sentence without cutting words up. This is a really useful tutorial.
http://www.jooria.com/Limit-Characters-From-Your-Text-a139.html
120

Create An Advanced Password Recovery Utility

Learn how to create a very advanced password recovery tool using PHP. This can be useful, and you can implement it in to your website login system.
http://net.tutsplus.com/tutorials/php/creating-an-advanced-password-recovery-utility/

Login To Analytics API Using PHP

Great little tip / trick that teaches you how to login to the Google Analytics API using ClientLogin.
http://www.electrictoolbox.com/google-analytics-login-php-curl-username-password/

Error 404 Pages With PHP Auto-Mailer

An awesome tutorial for creating a custom error 404 page.
http://net.tutsplus.com/tutorials/php/404403-website-error-pages-with-php-auto-mailer/

Resession (Session Manager)

Check this out. Its a really nifty session manager that you can integrate with your website.
http://www.milesj.me/resources/script/session-manager
219

Web Contact Forms With PHP

Learn how to create web radio buttons, and checkboxes using PHP.

Simple And Minimalistic User System

This tutorial will teach you how to create a minimalistic user system for a website from scratch.
http://rmb-scripting.com/tutorials.php?tutorial&tid=371

Simple PHP Class For Parsing Markup

This tutorial will teach you how to make a simple class that wraps PHP’s various regex functions in a fluent interface.
http://www.prodevtips.com/2009/03/26/simple-php-class-for-parsing-markup/

PHP Based Address Book Using MySQL

Learn how to create a PHP address book and store all the addresses in a MySQL database.
http://hubpages.com/hub/Simple-PHP-web-based-address-book-using-MySql

Single Signon Using OpenID, PHP and MySQL

Enable your website / web application to accept OpenID as a way of login.
http://www.webdesignermag.co.uk/tutorials/enabling-single-signon-using-open-id-login-php-and-mysql/
37

Creating a File & Downloading It Using PHP

In this tutorial, you will learn how to create a file using PHP, and then enable your users to download it.
http://pfmashup.tumblr.com/post/90347622/creatingfiledownloadingitinphp

Creating A Shopping Cart Using PHP

This tutorial will teach you how to create a simple and easy to use shopping cart to use on your website. There are 3 parts, enjoy!
Part 1
/ Part 2 / Part 3

Zero Fill a Number

Learn how to make a number a certain amount of characters. Such as 5 you could make to 005.
http://www.wlscripting.com/tutorial/55

Make a Unique Hits Counter

This tutorial will teach you how to create a unique hits counter, and keep the values permanently.
http://designkai.com/unique-hits-counter/

Tutorial Management System

Learn in 5 parts, how to create a fully functional tutorial management system, including comments!
Part 1 / Part 2 / Part 3 / Part 4 / Part 5

Thursday, November 10, 2011

Login with Facebook and Twitter

Facebook and Twitter have become large in the social network world and both networks offering oAuth support. We developed a system to login with Twitter and Facebook. Nowadays web users not interested to filling the big registration forms. This script helps you to avoid registration forms, It’s is very useful and simple to integrate.

Facebook Twitter Login


Download Script     Old Live Demo     New Live Demo

Database
Sample database users table columns id, email, oauth_uid, oauth_provider and username.
CREATE TABLE users
(
id INT PRIMARY KEY AUTO_INCREMENT,
email VARCHAR(70),
oauth_uid VARCHAR(200),
oauth_provider VARCHAR(200),
username VARCHAR(100),
twitter_oauth_token VARCHAR(200),
twitter_oauth_token_secret VARCHAR(200)
);

The tutorial contains three folders called facebook,twitter and config with PHP files.
facebook //Facebook OAUTH library
twitter //Twitter OAUTH library
config
-- functions.php 
-- dbconfig.php //Database connection
-- fbconfig.php //Facebook API connection
-- twconfig.php //Twitter API connection
index.php
home.php
login-twitter.php
login-facebook.php
getTwitterData.php

Facebook Setup
You have to create a application. Facebook will provide you app id and app secret id, just modify following code
fcconfig.php

define('APP_ID', 'Facebook APP ID');
define('APP_SECRET', 'Facebook Secret ID');
?>

Twitter Setup
Create a twitter application click here. Some like Facebook Twitter provide you consumer key amd consumer secret key using these modify following code.
twconfig.php

define('YOUR_CONSUMER_KEY', 'Twitter Key');
define('YOUR_CONSUMER_SECRET', 'Twitter Secret Key');
?>

dbconfig.php
Database configuration file.

define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'User Name');
define('DB_PASSWORD', 'Password');
define('DB_DATABASE', 'DATABASE');
$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
$database = mysql_select_db(DB_DATABASE) or die(mysql_error());
?>

login-twitter.php
In root directory find out the below line at login-twitter.php code and replace yourwebsite.
$request_token = $twitteroauth->getRequestToken('http://yourwebsite.com/getTwitterData.php');

index.php
If you want to modify your web project existing login or index pages, just use following code.

session_start();
if (isset($_SESSION['id'])) {
// Redirection to login page twitter or facebook
header("location: home.php");
}
if (array_key_exists("login", $_GET))
{
$oauth_provider = $_GET['oauth_provider'];
if ($oauth_provider == 'twitter')
{
header("Location: login-twitter.php");
}
else if ($oauth_provider == 'facebook')
 {
header("Location: login-facebook.php");
}
}
?>
//HTML Code
?login&oauth_provider=twitter">Twitter_Login
?login&oauth_provider=facebook">Facebook_Login

PHP Image and Text Watermark

We received many tutorial requests from 9lessons readers that asked how to generate watermark image using PHP. In this post Arun Kumar Sekar coded two functions such as watermark_text() and watermark_image() to generate text and images watermarks on images. Integrate this to your web project upload image system and produce copyright photos.

PHP Image Watermark

Download Script     Text Live Demo     Image Live Demo

Watermark with Text - watermark_text()
Text watermark function, here you have to configure font source, size and text. live demo
$font_path = "GILSANUB.TTF"; // Font file
$font_size = 30; // in pixcels
$water_mark_text_2 = "9lessons"; // Watermark Text

function watermark_text($oldimage_name, $new_image_name)
{
global $font_path, $font_size, $water_mark_text_2;
list($owidth,$oheight) = getimagesize($oldimage_name);
$width = $height = 300;
$image = imagecreatetruecolor($width, $height);
$image_src = imagecreatefromjpeg($oldimage_name);
imagecopyresampled($image, $image_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
$blue = imagecolorallocate($image, 79, 166, 185);
imagettftext($image, $font_size, 0, 68, 190, $blue, $font_path, $water_mark_text_2);
imagejpeg($image, $new_image_name, 100);
imagedestroy($image);
unlink($oldimage_name);
return true;
}

Watermark with Image - watermark_image()
Image watermark function, here you have to configure image water mark source. live demo
$image_path = "9lesson.png";

function watermark_image($oldimage_name, $new_image_name)
/{
global $image_path;
list($owidth,$oheight) = getimagesize($oldimage_name);
$width = $height = 300;
$im = imagecreatetruecolor($width, $height);
$img_src = imagecreatefromjpeg($oldimage_name);
imagecopyresampled($im, $img_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
$watermark = imagecreatefrompng($image_path);
list($w_width, $w_height) = getimagesize($image_path);
$pos_x = $width - $w_width;
$pos_y = $height - $w_height;
imagecopy($im, $watermark, $pos_x, $pos_y, 0, 0, $w_width, $w_height);
imagejpeg($im, $new_image_name, 100);
imagedestroy($im);
unlink($oldimage_name);
return true;
}


Image Form
Contains simple PHP and HTML form submit code. Here HTML form sending post request.

$demo_image= "";
if(isset($_POST['createmark']) and $_POST['createmark'] == "Submit")
{
$path = "uploads/";
$valid_formats = array("jpg", "bmp","jpeg");
$name = $_FILES['imgfile']['name'];
if(strlen($name))
{
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats) && $_FILES['imgfile']['size'] <= 256*1024)
{
$upload_status = move_uploaded_file($_FILES['imgfile']['tmp_name'], $path.$_FILES['imgfile']['name']);
if($upload_status){
$new_name = $path.time().".jpg";
// Here you have to user functins watermark_text or watermark_image
if(watermark_text($path.$_FILES['imgfile']['name'], $new_name))
$demo_image = $new_name;
}
}
else
$msg="File size Max 256 KB or Invalid file format.";
}
}
?>
// HTML Code

Upload Image
Image :file" name="imgfile" />

submit" name="createmark" value="Submit" />

if(!empty($demo_image))
echo '" />';
?>

Wednesday, November 9, 2011

How to upload a file using a model

First declare an attribute to store the file name in the model class (either a form model or an active record model). Also declare a file validation rule for this attribute to ensure a file is uploaded with specific extension name.
class Item extends CActiveRecord
{
    public $image;
    // ... other attributes
 
    public function rules()
    {
        return array(
            array('image', 'file', 'types'=>'jpg, gif, png'),
        );
    }
}
Then, in the controller class define an action method to render the form and collect user-submitted data.
class ItemController extends CController
{
    public function actionCreate()
    {
        $model=new Item;
        if(isset($_POST['Item']))
        {
            $model->attributes=$_POST['Item'];
            $model->image=CUploadedFile::getInstance($model,'image');
            if($model->save())
            {
                $model->image->saveAs('path/to/localFile');
                // redirect to success page
            }
        }
        $this->render('create', array('model'=>$model));
    }
}
Finally, create the action view and generate a file upload field.
 echo CHtml::form('','post',array('enctype'=>'multipart/form-data')); ?>
...
php echo CHtml::activeFileField($model, 'image'); ?>
...
php echo CHtml::endForm(); ?>

Tuesday, November 8, 2011

Video - Installing Yii Framework (OSX)


I'm going to start doing video tutorials, I know I enjoy watching and listening to people explain things, I think I'll do some more things in this format.