Showing posts with label Session. Show all posts
Showing posts with label Session. Show all posts

Thursday, April 29, 2010

Integrate Phpbb3 forum login(session) with website

Integrate Phpbb3 forum login(session) with website
When we use third part tool phpbb3 with your website as site forum there is necessary  to set phpbb3 login session when any user login to website so user can also use forum without re-login to phpbb3 forum because there is many session variable that is require for login to forum and work on it.
Example:-
Website name:-www.example.com
Website forum:- www.example.com/forum/
Website login page:-www.example.com/login.php
There is 3 way to reach to site forum first open/land to index.php page and reach to it’s viewforum.php page and reach to it’s viewtopic.php page
Website forum page use by user:-
www.example.com/forum/index.php,
First we need that user that is register with site also have entry in phpbb3 forum users table and user_group with same user id that is given on site registration time.
When we move site to forum any above forum page than we need to pass that user id as parameter and get that parameter then fetch record from site user table and set all necessary session parameter for keep login on form.



 Forum login session set code:-


//get site user Id
$var_id=$_GET['id'];
//start forum user session
$user->session_begin();
//check user id exist or login 
if($var_id!='')
{
$str="select * from site_members where member_id=$var_id"; 
$qstr=mysql_query($str,$dbc) or die('error in members'.mysql_error());
$fstr=mysql_fetch_array($qstr);
    
   $user->data[user_ip] = $_SERVER['REMOTE_ADDR'];
    $user->data[user_regdate] = 0;
    $user->data[username] = $fstr['first_name'];
    $user->data[username_clean] = $fstr['first_name'];
    $user->data[user_password] = md5($fstr['password']);
    $user->data[user_passchg] = 0;
    $user->data[user_pass_convert] = 0;
    $user->data[user_email] = '';
    $user->data[user_email_hash] = 0;
    $user->data[user_birthday] =  0- 0-   0;
    $user->data[user_lastvisit] = 1238142551;
    $user->data[user_lastmark] = 0;
    $user->data[user_lastpost_time] = 0;
    $user->data[user_lastpage] = index.php;
    $user->data[user_last_confirm_key] = '';
    $user->data[user_last_search] = 0;
    $user->data[user_warnings] = 0;
    $user->data[user_last_warning] = 0;
    $user->data[user_login_attempts] = 0;
    $user->data[user_inactive_reason] = 0;
    $user->data[user_inactive_time] = 0;
    $user->data[user_posts] = 1;
    $user->data[user_lang] = en;
    $user->data[user_timezone] = 0.00;
    $user->data[user_dst] = 0;
    $user->data[user_dateformat] = 'd M Y H:i';
    $user->data[user_style] = 1;
    $user->data[user_rank] = 0;
    $user->data[user_colour] = '';
    $user->data[user_new_privmsg] = 0;
    $user->data[user_unread_privmsg] = 0;
    $user->data[user_last_privmsg] = 0;
    $user->data[user_message_rules] = 0;
    $user->data[user_full_folder] = -3;
    $user->data[user_emailtime] = 0;
    $user->data[user_topic_show_days] = 0;
    $user->data[user_topic_sortby_type] = t;
    $user->data[user_topic_sortby_dir] = d;
    $user->data[user_post_show_days] = 0;
    $user->data[user_post_sortby_type] = t;
    $user->data[user_post_sortby_dir] = a;
    $user->data[user_notify] = 1;
    $user->data[user_notify_pm] = 0;
    $user->data[user_notify_type] = 0;
    $user->data[user_allow_pm] = 1;
    $user->data[user_allow_viewonline] = 1;
    $user->data[user_allow_viewemail] = 0;
    $user->data[user_allow_massemail] = 1;
    $user->data[user_options] = 831;
    $user->data[user_avatar] = '';
    $user->data[user_avatar_type] = 0;
    $user->data[user_avatar_width] = 0;
    $user->data[user_avatar_height] = 0;
    $user->data[user_sig] = '';
    $user->data[user_sig_bbcode_uid] = '3m872xlu';
    $user->data[user_sig_bbcode_bitfield] = 0;
    $user->data[user_from] = '';
    $user->data[user_icq] = '';
    $user->data[user_aim] = '';
    $user->data[user_yim] = '';
    $user->data[user_msnm] = '';
    $user->data[user_jabber] = '';
    $user->data[user_website] = '';
    $user->data[user_occ] = '';
    $user->data[user_interests] = '';
    $user->data[user_actkey] = '';
    $user->data[user_newpasswd] = '';
    $user->data[user_form_salt] = '168ff8093150140f';
    $user->data[session_id] = session_id();
    $user->data[session_user_id] = $fstr["member_id"];;
    $user->data[session_forum_id] = 0;
    $user->data[session_last_visit] = '1238142551';
    $user->data[session_start] = time();
    $user->data[session_time] = time();
    $user->data[session_ip] = $_SERVER['REMOTE_ADDR'];
    $user->data[session_browser] = $_SERVER['HTTP_USER_AGENT'];
    $user->data[session_forwarded_for] = '';
    $user->data[session_page] = 'index.php';  
    $user->data[session_viewonline] = 1;
    $user->data[session_autologin] = 0;
    $user->data[session_admin] = 0;
    $user->data[is_registered] = 1;
    $user->data[is_bot] = '';
    $autologin='true';
    $result = $auth->login($fstr['username'], $fstr['password'], $autologin);
    $result['error_msg']=LOGIN_SUCCESS;
}




Note-

1.    $user->data[session_page] = 'index.php';  

index.php is the value according to page on which it is use like for viewforum.php it will be viewforum.php and for viewtopic.php it will be viewtopic.php

2.
$auth->login($username, $password, $remember, 1, 0)


$username would be the exact username that would be found in the phpBB user table.

$password would be the string to match by (pre-hash, this should be just the original text, the $auth->login() function takes care of converting it into a comparable hash)

$remember is a boolean value, false if no remember me choice, true if user is going to have a 'remembered' session

Saturday, November 14, 2009

How to Use a CAPTCHA in php

To put it simply a captcha works by generating a random string, writing it to an image, then storing the string inside of a session or cookie or by some other method. This is then checked when the form or operation is performed.
Their are 7 basic Step
  1. Random text generated
  2. Text written to image
  3. Text stored in session/cookie/database
  4. Image displayed to user
  5. User enters the code
  6. User entered code is checked against the stored key
  7. If they match then something is done
Random text

I will use the php functions, microtime() and mktime() to generate a number. This number will then be encrypted using md5(). With this 32 character long encrypted string we will then use substr() to cut it down to a 5 letter long string. This is our random text.


//Start the session so we can store what the code actually is.
session_start();

//Now lets use md5 to generate a totally random string
$md5 = md5(microtime() * mktime());

/*
We dont need a 32 character long string so we trim it down to 5

*/

$string = substr($md5,0,5);
?>


Text to the image


/*
Now for the GD stuff, for ease of use lets create

the image from a background image.

*/


$captcha = imagecreatefrompng("./captcha.png");

/*
Lets set the colours, the colour $line is used to generate lines.

Using a blue misty colours. The colour codes are in RGB

*/


$black = imagecolorallocate($captcha, 0, 0, 0);
$line = imagecolorallocate($captcha,233,239,239);

/*
Now to make it a little bit harder for any bots to break,

assuming they can break it so far. Lets add some lines

in (static lines) to attempt to make the bots life a little harder

*/

imageline($captcha,0,0,39,29,$line);
imageline($captcha,40,0,64,29,$line);
?>


Text stored in session/cookie


/*
Now for the all important writing of the randomly generated string to the image.

*/

imagestring($captcha, 5, 20, 10, $string, $black);


/*
Encrypt and store the key inside of a session

*/


$_SESSION['key'] = md5($string);

/*
Output the image

*/

header("Content-type: image/png");
imagepng($captcha);

?>

Image displayed to user/User Enter COde

User simple img tag and input box


Check Enter Code is correct or not


session_start
();

//Encrypt the posted code field and then compare with the stored key

if(md5($_POST['code']) != $_SESSION['key'])
{

die(
"Error: You must enter the code correctly");
}else{

echo
'You entered the code correctly';
}
?>