Thursday, June 11, 2009

How can store session in data base

Step1: Create A Table:
CREATE TABLE `sessions` (
`session_key` char(32) NOT NULL,
`session_expire` int(11) unsigned NOT NULL,
`session_value` text NOT NULL,
PRIMARY KEY (`session_key`)
)

Step2 : Create A connection file
configure.php
$HOST = "localhost";
$DBNAME="sessions";
$USER = "nanhe";
$PASS = "chapra";
$HANDLER = "";
$LIFETIME = get_cfg_var("session.gc_maxlifetime");
?>

Step3 : Create handler
handler.php
include("configure.php");
function sessionOpen($save_path, $session_name){

global $HOST, $DBNAME, $USER, $PASS, $HANDLER;

if (!$HANDLER = mysql_pconnect($HOST, $USER, $PASS)) {
echo("
  • Can't connect to $HOST as $USER");
    echo("
  • MySQL Error: ". mysql_error());
    die;
    }

    if (! mysql_select_db($DBNAME, $HANDLER)) {
    echo("
  • We were unable to select database $DBNAME");
    die;
    }

    return true;
    }

    function sessionClose()
    {

    return true;
    }

    function sessionRead($session_key)
    {
    global $session;
    $session_key = addslashes($session_key);

    $session_session_value =
    mysql_query("SELECT session_value
    FROM sessions WHERE session_key = '$session_key'")
    or die(mysql_error());

    if (mysql_numrows($session_session_value) == 1) {
    return mysql_result($session_session_value, 0);
    } else {
    return false;
    }
    }

    function sessionWrite($session_key, $val)
    {
    global $session;
    echo "The value=".$val;
    $session_key = addslashes($session_key);
    $val = addslashes($val);
    $session = mysql_result(mysql_query("SELECT COUNT(*) FROM sessions
    WHERE session_key = '$session_key'"), 0);

    if ($session == 0) {
    $return =
    mysql_query("INSERT INTO sessions
    (session_key, session_expire, session_value)
    VALUES ('$session_key',
    UNIX_TIMESTAMP(NOW()), '$val')")
    or die(mysql_error());
    } else {
    $return = mysql_query("UPDATE sessions
    SET session_value = '$val',
    session_expire = UNIX_TIMESTAMP(NOW())
    WHERE session_key = '$session_key'")
    or die(mysql_error());

    if (mysql_affected_rows() < 0) {
    echo("We were unable to update session
    session_value for session $session_key");
    }
    }
    return $return;
    }


    function sessionDestroy($session_key)
    {
    global $session;
    $session_key = addslashes($session_key);

    $return = mysql_query("DELETE FROM sessions
    WHERE session_key = '$session_key'")
    or die(mysql_error());
    return $return;
    }

    function sessionGc($maxlifetime)
    {
    global $session;
    $expirationTime = time() - $maxlifetime;

    $return = mysql_query("DELETE FROM sessions WHERE session_expire <
    $expirationTime") or die("sessionGc Function".mysql_error());
    return $return;
    }

    session_set_save_handler("sessionOpen", "sessionClose", "sessionRead", "sessionWrite", "sessionDestroy", "sessionGc");


    ?>
    Step4 : Test your program
  • Thursday, May 28, 2009

    Monday, May 4, 2009

    create a blank page in phpbb

    /*
    * phpBB3 blank example page.
    */

    define('IN_PHPBB', true);
    $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
    $phpEx = substr(strrchr(__FILE__, '.'), 1);
    include($phpbb_root_path . 'common.' . $phpEx);
    // Start session management
    $user->session_begin();
    $auth->acl($user->data);
    $user->setup('common');
    // include page language file
    $user->add_lang('page');
    page_header($user->lang['PAGE_TITLE']);

    /*
    Put your own code here
    */

    $template->set_filenames(array(
    'body' =>'nanhe_kumar.html'
    ));
    // parse page:
    page_footer();
    ?>

    how can your link session with two sites

    if you want login in
    www.gradtrain.com
    and my session is also available in
    gradtrain.com/forum
    ?php
    define('IN_PHPBB', true);
    define('ROOT_PATH', "forum/");

    if (!defined('IN_PHPBB') || !defined('ROOT_PATH')) {
    exit();
    }

    $phpEx = "php";
    $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : ROOT_PATH . '/';
    include($phpbb_root_path . 'common.' . $phpEx);

    $user->session_begin();
    $auth->acl($user->data);
    ?>
    $auth->login('nanhe', 'nanhe', $remember, 1, 0)
    ?>
    $user->session_kill();
    $user->session_begin();
    ?>

    how can login in phpbb, forum with own created page

    Suppose your site is
    www.gradtrain.com
    and your forum is www.gradtrain.com/forum
    and if want login with your crated page
    www.gradtrain.com/forum-login.php
    then take this step
    define('IN_PHPBB', true);
    define('PHPBB_ROOT_PATH', './forum/');

    $phpbb_root_path = 'forum/';

    $phpEx = substr(strrchr(__FILE__, '.'), 1);
    include($phpbb_root_path . 'common.' . $phpEx);
    include($phpbb_root_path . 'includes/functions_display.' . $phpEx);

    // Start session management
    $user->session_begin();
    $auth->acl($user->data);
    ?>
    /* Start Logout*/
    if ($user->data['is_registered']){

    echo("
    data['session_id']."'>");
    echo("");
    echo("");
    echo("
    ");
    }
    /*End Logout*/

    /*Start Login Box*/
    if (!$user->data['is_registered']){
    echo("
    ");
    echo(" Username:");
    echo("
    ");
    echo(" ");
    echo("

    ");
    echo(" Password:");
    echo("
    ");
    echo(" ");
    echo("

    ");
    echo("");
    echo("");
    //echo("");
    echo("
    ");
    }
    /*End Login Box*/

    ?>

    Friday, May 1, 2009

    The command prompt has been disabled by your administrator

    When you attempt to run CMD.exe or a batch file, you may receive the message "The command prompt has been disabled by your administrator". This is caused by restrictions placed in Registry. DisableCMD value is set to 1 or via Group Policy. To enable Task Manager, try any of these methods:

    Method 1: Using the console registry tool

    • Click Start, Run and type this command exactly as given below: (better - Copy and paste)

    Method 2: Edit the registry directly

    • Open Registry Editor (Regedit.exe) and navigate to:

    [HKEY_CURRENT_USER\Software\Policies\Microsoft\Windows\System]

    • In the right-pane, double-click DisableCMD and set it's data to 0

    Method 3: Using Group Policy Editor in Windows XP Professional.

    • Click Start, Run, type gpedit.msc and click OK.

    • Navigate to User Configuration \ Administrative Templates \ System

    • Double-click the Prevent access to the command prompt

    You can then disable or set the policy to Not Configured. Disabling or setting this policy to Not Configured should solve the problem.

    Related articles

    "Registry Editing has been disabled by your administrator"
    "Task Manager has been disabled by your administrator"

    Monday, April 27, 2009

    Hide .php extension with url rewriting using .htaccess

    Last time I’ve written an article about hiding php file extension where I’ve showed you how you can use .html or .asp extension of file instead of .php extension. But there was one flaw in that technique you have had to change the file extension explicitly but in this post I’m going to show you how to rewrite the URL instead of renaming the file extension Using this technique you will see product.html in the address bar of the browser but the actual file name remains product.php and you don’t need to rename the file extension. Furthermore you can rewrite the URL like product.php?id=5 to product-5.html.

    what is the benefits of rewriting URL?

    When a search engine visits the dynamic url like product.php?id=5 it does not give much importance to that URL as search engine sees “?” sign treat it as a url which keeps on changing. so we’re converting the dynamic URL like the product.php?id=5 to static url format like product-5.html. We’ll rewrite the url in such a way that in browser’s address bar it will display as a product-5.html but it actually calls the file product.php?id=5. So that why these kind of URL also named as SEO friendly URL.

    what is required for URL rewriting ??

    To rewrite the URL you must have the mod_rewrite module must be loaded in apache server. And furthermore, FollowSymLinks options also need to be enabled otherwise you may encounter 500 Internal Sever Error. If you don’t know much about mod_rewrite module then please check this post to know how to check and enable mod_rewrite module in apache?

    Examples of url rewriting for seo friendly URL

    For rewriting the URL, you should create a .htaccess file in the root folder of your web directory. And have to put the following codes as your requirement.

    Options +FollowSymlinks
    RewriteEngine on
    RewriteRule ^(.*)\.htm$ $1.php [nc]

    The following example will rewrite the test.php to test.html i.e when a URL like http://localhost/test.htm is called in address bar it calls the file test.php. As you can see the regular expression in first part of the RewriteRule command and $1 represents the first regular expression of the part of the RewriteRule and [nc] means not case sensitive.

    Options +FollowSymlinks
    RewriteEngine on
    RewriteRule ^product-([0-9]+)\.html$ products.php?id=$1

    The following example will rewrite the product.php?id=5 to porduct-5.html i.e when a URL like http://localhost/product-5.html calls product.php?id=5 automatically.