Developers Archive for March, 2007

Session in PHP

Session in PHP Wednesday, March 21st, 2007

Session in PHP

In PHP, Session is used to store the information temporarily for a particular session time. The usage of this session are shown below.

Starting a PHP Session

It is a first step in using a session, before storing anything is session, you must start the session. The syantax is

session_start();
It start up your PHP session. It must be at the very beginning of your code, before any HTML content.

Now the session is started, We can set any number of variables and values in the particular session, by assigning in the associative array $_SESSION, It Syntax is

$_SESSION[’variable’] = value;

Example:

$_SESSION[’username’]=’aaa’;
$_SESSION[’password’] = ‘pass’;

here, We can retrieve these values in any of the page in this session. by using

echo $_SESSION[’username’]; // It displays aaa
We can also retrieve this by using print_r by print_r($_SESSION);

It displays
Array
(
[username] => aaa
[password] => pass
)

Session Name

We also set the name for the particular session by using session_name, Its syntax is

string session_name ( [string name])

By using this function, we can both set and get the name of the session.

session_name() returns the name of the current session. If name is specified, the name of the current session is changed to its value.

Example :

To Set the name of the Session

session_name(”newsession”);

To get the Session name

$sesname = session_name();
It returns the name of the current session.

Session ID

Similarly We also set and get the Id for the Particular session. Its Syntax is

string session_id ( [string id])

session_id() returns the session id for the current session. If id is specified, it will replace the current session id.

Session Destroy

After Using the Session, we destroy it, for safe. It Syntax is

bool session_destroy ( void)

It destroy the all data registered in the session.

session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie.

This function returns TRUE on success and FALSE on failure to destroy the session data.

Sample Code

<?php
session_start();
$_SESSION[’eno’] = 101;
$id = session_id();
session_name(”test”);
print_r($_SESSION);
session_destroy();
?>

FROM_UNIXTIME()

FROM_UNIXTIME() Wednesday, March 21st, 2007

FROM_UNIXTIME() functions return values in the connection’s current time zone, which is available as the value of the time_zone system variable.If we use FROM_UNIXTIME() to convert between TIMESTAMP values and Unix time stamp values, the conversion is lossy because the mapping is not one-to-one in both directions.

Syntax:
FROM_UNIXTIME([timestamp])

Example:
select from_unixtime(1174374559);

Truncate in Smarty

Truncate in Smarty Wednesday, March 21st, 2007

This truncates a variable to a character length, default is 80. As an optional second parameter, we can specify a string of text to display at the end if the variable was truncated. The characters in the string are included with the original truncation length. By default, truncate will attempt to cut off at a word boundary. If we want to cut off at the exact character length, pass the optional third parameter of true.

Example

<?php

$smarty->assign(’input’, ‘This is test string which use truncate.’);

?>

In template we can have like,

{$input}

{$input|truncate}

{$input|truncate:30:”—”}

{$input|truncate:30:”…”:true}

The output will be as follows:

This is test string which use truncate.

This is test string which use truncate.

This is test—

This is test st…


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.