27 February 2011

Sessions in Php - Php For Beginners (Lesson 9)

What are sessions variables?
Session variables are just like normal variables except the fact that session variables are used to access some particular data across multiple pages. For example if you want to build a login system then in order to check whether the user is logged in or not you have to use session variables. You may store the user id in the session variable for each user at the time of signin/signup and then use the value of this session variable across different pages of members area.

How to use session variables in php?
In php if you want to use session variables then first of all you should start the session. This can be done by adding the following code at the start of each page where you want to use session variables:
<?php 
session_start(); 
?>
After adding the above mentioned code at the start of each page you are all set for using session variables. Session variables in php can be easily declared using the following code:
<?php
$_SESSION['user_id']=1234; 
?>
After the declaration of session variables you can access the value of these variables on any page using the following code:
<?php
echo $_SESSION['user_id']; 
?>
Now that you know how to declare and use session variables you should know how to remove values from session variables. Removing a value from any session variable is very simple, you can use following code:
<?php
unset($_SESSION['user_id']); 
?>
But the above code will only remove the value of one session variable, now if you have declared many session variables and you want to delete them all at once then you can do this by completely destroying the session. This can be done by using the following code:
<?php
session_destroy(); 
?>
Ok guyz I guess that is all about session variables in php. If you have any queries or suggestions then please feel free to comment.

Note: Dont forget to start the session at the beginning of each page.


next Previous Lesson --- Next Lesson next

05 February 2011

How to configure Reporting Services 2008 in SharePoint 2010

Requirments :
You should have SP1 CU7 to get RS 2008 to work with SP 2010.

Please follow these steps to integrate reportaing services with SharePoint:

1) Login to the Reporting Services server and make sure that the service is up and running, and double check that the reporting server is working:
  a) Open SQL Reporting configuration manager
  b) Select Report Manager Url.
  c) click on the url to make sure it is working.
For example: http://SERVERNAME/Reports/

2) Open SharePoint Central administration:
  a) Select General Application Settings.
  b) Select Reporting Services Integration.
  c) Set the report server url.
  d) Select authentication mode, Windows if the reports show personalized data.
  OR select the trusted account that will be used for impersonation.
  e) Click on Ok, You will get a successful page.
  f) You might get the following error:
"Failed to establish connection with report server. Verify the server URL is correct or review ULS logs for more information. Product area: SQL Server Reporting Services, Category: Configuration Pages"
This means that the SSRS is not configured to run with SharePoint Integrated Mode, to change it,read below article "Overview of Configuration Steps on a Report Server":
http://technet.microsoft.com/en-us/library/bb326356.aspx

3) Go to your sharepoint site settings and enable Report Integration feature on site collection features.
4) Go to your site and add reporting services webpart in your page and configure it to your report.

Note: It is recommended to have a library that has all your reports in one place and all SQL Reporting services webparts are connecting to this library.

01 February 2011

How to make thumbnails in php

Sometimes we need to resize uploaded images and make their thumbnails so that the page may load faster. So in this post I am sharing a very simple and easy script which can be used by anyone to generate thumbnails or resize images.

This script uses a class which should be included on the page where you want to make thumbnail or resize the original image. This class should be saved as thumb_class.php, its complete code is mentioned below:

thumb_class.php
<?php
// Imaging
class imaging
{
    // Variables
    private $img_input;
    private $img_output;
    private $img_src;
    private $format;
    private $quality = 80;
    private $x_input;
    private $y_input;
    private $x_output;
    private $y_output;
    private $resize;

    // Set image
    public function set_img($img)
    {
        // Find format
        $ext = strtoupper(pathinfo($img, PATHINFO_EXTENSION));
        // JPEG image
        if(is_file($img) && ($ext == "JPG" OR $ext == "JPEG"))
        {
            $this->format = $ext;
            $this->img_input = ImageCreateFromJPEG($img);
            $this->img_src = $img;
        }
        // PNG image
        elseif(is_file($img) && $ext == "PNG")
        {
            $this->format = $ext;
            $this->img_input = ImageCreateFromPNG($img);
            $this->img_src = $img;
        }
        // GIF image
        elseif(is_file($img) && $ext == "GIF")
        {
            $this->format = $ext;
            $this->img_input = ImageCreateFromGIF($img);
            $this->img_src = $img;
        }
        // Get dimensions
        $this->x_input = imagesx($this->img_input);
        $this->y_input = imagesy($this->img_input);
    }

    // Set maximum image size (pixels)
    public function set_size($max_x = 100,$max_y = 100)
    {
        // Resize
        if($this->x_input > $max_x || $this->y_input > $max_y)
        {
            $a= $max_x / $max_y;
            $b= $this->x_input / $this->y_input;
            if ($a<$b)
            {
                $this->x_output = $max_x;
                $this->y_output = ($max_x / $this->x_input) * $this->y_input;
            }
            else
            {
                $this->y_output = $max_y;
                $this->x_output = ($max_y / $this->y_input) * $this->x_input;
            }
            // Ready
            $this->resize = TRUE;
        }
        // Don't resize      
        else { $this->resize = FALSE; }
    }
    // Set image quality (JPEG only)
    public function set_quality($quality)
    {
        if(is_int($quality))
        {
            $this->quality = $quality;
        }
    }
    // Save image
    public function save_img($path)
    {
        // Resize
        if($this->resize)
        {
            $this->img_output = ImageCreateTrueColor($this->x_output, $this->y_output);
            ImageCopyResampled($this->img_output, $this->img_input, 0, 0, 0, 0, $this->x_output, $this->y_output, $this->x_input, $this->y_input);
        }
        // Save JPEG
        if($this->format == "JPG" OR $this->format == "JPEG")
        {
            if($this->resize) { imageJPEG($this->img_output, $path, $this->quality); }
            else { copy($this->img_src, $path); }
        }
        // Save PNG
        elseif($this->format == "PNG")
        {
            if($this->resize) { imagePNG($this->img_output, $path); }
            else { copy($this->img_src, $path); }
        }
        // Save GIF
        elseif($this->format == "GIF")
        {
            if($this->resize) { imageGIF($this->img_output, $path); }
            else { copy($this->img_src, $path); }
        }
    }
    // Get width
    public function get_width()
    {
        return $this->x_input;
    }
    // Get height
    public function get_height()
    {
        return $this->y_input;
    }
    // Clear image cache
    public function clear_cache()
    {
        @ImageDestroy($this->img_input);
        @ImageDestroy($this->img_output);
    }
}
class thumbnail extends imaging {
    private $image;
    private $width;
    private $height;
   
    function __construct($image,$width,$height,$dir) {
parent::set_img($image);
parent::set_quality(80);
parent::set_size($width,$height);
$image_temp=explode('/',$image);
$image_temp1=$image_temp[count($image_temp)-1];
$this->thumbnail= $dir.$image_temp1;
parent::save_img($this->thumbnail);
parent::clear_cache();
        }
    function __toString() {
            return $this->thumbnail;
    }
}
?>
Example Usage

After saving this file as thumb_class.php you can use it as follows:
<?php
include("thumb_class.php");
new thumbnail('test.jpg',400,300,'thumb/');
?>
In the above example
"test.php" is the name of image you want to resize,
"400" is the new width of image,
"300" is the new height of image, and
"thumb/" is the destination folder.

For any queries, suggestions or problems that you may have, feel free to comment on this post, I would love to answer you guyz ;).