Page 1 of 1

ระบบสมัครสมาชิก ผมอยากมันมันลิงค์กับ เซิฟเวอร์เกม ทำไง

Posted: Tue Apr 05, 2011 8:10 am
by zafearrose
ใน localhost/phpmyadmin > ประกอบไปด้วย ฐานข้อมูล เวปjoomla แล้วก็ ฐานข้อมูลเกมส์ออนไลน์

ฐานข้อมูล joomlaz ผมตั่งชื่อไว้ romane (33)
ฐานข้อมูลเกมส์ออนไลน์ ผมตั่งไว้ game (34)

Image

คำถามคือว่า ระบบสมัครสามาชิกในเวปไซค์ ผมอยากทำให้มันเป็นการสมัครสมาชิกในเกมด้วยต้องทำไง

Re: ระบบสมัครสมาชิก ผมอยากมันมันลิงค์กับ เซิฟเวอร์เกม ทำไง

Posted: Tue Apr 05, 2011 7:22 pm
by gamaza
เนื่องจากผมไม่เห็น field ที่อยู่ใน table login เลยกำหนดเป็นแบบนี้ละกัน

Code: Select all

CREATE TABLE `login` (
`id`  int(11) NOT NULL AUTO_INCREMENT ,
`name`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' ,
`username`  varchar(150) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' ,
`email`  varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' ,
`password`  varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' ,
PRIMARY KEY (`id`),
INDEX `idx_name` USING BTREE (`name`),
INDEX `username` USING BTREE (`username`),
INDEX `email` USING BTREE (`email`)
)
ENGINE=MyISAM
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
AUTO_INCREMENT=75
CHECKSUM=0
ROW_FORMAT=DYNAMIC
DELAY_KEY_WRITE=0
;
หมายเหตุ นี้เป็นการแก้ไข core component ของ joomla ซื่งอาจทำให้ไม่สร้าง update joomla เป็น version ใหม่
หรือ update patch ได้ หรืออาจมีปัญหากับ component อื่น ๆ ได้ เพราะผมยังไม่ได้ทดสอบทางด้านนี้
code ที่เขียน เพิ่มมานี้ได้ผ่านการทดสอบมาเพียงไม่กี่ร้อยครั้ง ซื่งแต่ละครั้งที่ทดสอบก็ผ่านด้วยดี แต่ผมไม่อาจแน่ใจได้ว่า
มันจะมี bug หรือไม่ ในระหว่างทดสอบควรสร้าง web ใหม่มาทดสอบ ถ้าเกิดนำไปใช้งานแล้วมีปัญหากรุณาแจ้งให้ทราบด้วย

สมมุติว่า site เป็น localhost\joomla

ส่วนแรกเป็นการแก้ไข code component users ในส่วนของ front end หน้าสมัครสมาชิก
ให้เข้าไปที่ localhost\joomla\components\com_user\controller.php
เปิดขึ้นมา มองหา function register_save

จาก code นี้

Code: Select all

function register_save()
	{
		global $mainframe;

		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
		
		// Get required system objects
		$user 		= clone(JFactory::getUser());
		$pathway 	=& $mainframe->getPathway();
		$config		=& JFactory::getConfig();
		$authorize	=& JFactory::getACL();
		$document   =& JFactory::getDocument();

		// If user registration is not allowed, show 403 not authorized.
		$usersConfig = &JComponentHelper::getParams( 'com_users' );
		if ($usersConfig->get('allowUserRegistration') == '0') {
			JError::raiseError( 403, JText::_( 'Access Forbidden' ));
			return;
		}

		// Initialize new usertype setting
		$newUsertype = $usersConfig->get( 'new_usertype' );
		if (!$newUsertype) {
			$newUsertype = 'Registered';
		}

		// Bind the post array to the user object
		if (!$user->bind( JRequest::get('post'), 'usertype' )) {
			JError::raiseError( 500, $user->getError());
		}

		// Set some initial user values
		$user->set('id', 0);
		$user->set('usertype', $newUsertype);
		$user->set('gid', $authorize->get_group_id( '', $newUsertype, 'ARO' ));

		$date =& JFactory::getDate();
		$user->set('registerDate', $date->toMySQL());

		// If user activation is turned on, we need to set the activation information
		$useractivation = $usersConfig->get( 'useractivation' );
		if ($useractivation == '1')
		{
			jimport('joomla.user.helper');
			$user->set('activation', JUtility::getHash( JUserHelper::genRandomPassword()) );
			$user->set('block', '1');
		}

		// If there was an error with registration, set the message and display form
		if ( !$user->save() )
		{
			JError::raiseWarning('', JText::_( $user->getError()));
			$this->register();
			return false;
		}

		// Send registration confirmation mail
		$password = JRequest::getString('password', '', 'post', JREQUEST_ALLOWRAW);
		$password = preg_replace('/[\x00-\x1F\x7F]/', '', $password); //Disallow control chars in the email
		UserController::_sendMail($user, $password);

		// Everything went fine, set relevant message depending upon user activation state and display message
		if ( $useractivation == 1 ) {
			$message  = JText::_( 'REG_COMPLETE_ACTIVATE' );
		} else {
			$message = JText::_( 'REG_COMPLETE' );
		}
		
		$this->setRedirect('index.php', $message);
	}
แก้เป็น

Code: Select all

function register_save()
	{
		global $mainframe;

		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
		
		// Get required system objects
		$user 		= clone(JFactory::getUser());
		$pathway 	=& $mainframe->getPathway();
		$config		=& JFactory::getConfig();
		$authorize	=& JFactory::getACL();
		$document   =& JFactory::getDocument();

		// If user registration is not allowed, show 403 not authorized.
		$usersConfig = &JComponentHelper::getParams( 'com_users' );
		if ($usersConfig->get('allowUserRegistration') == '0') {
			JError::raiseError( 403, JText::_( 'Access Forbidden' ));
			return;
		}

		// Initialize new usertype setting
		$newUsertype = $usersConfig->get( 'new_usertype' );
		if (!$newUsertype) {
			$newUsertype = 'Registered';
		}

		// Bind the post array to the user object
		if (!$user->bind( JRequest::get('post'), 'usertype' )) {
			JError::raiseError( 500, $user->getError());
		}

		// Set some initial user values
		$user->set('id', 0);
		$user->set('usertype', $newUsertype);
		$user->set('gid', $authorize->get_group_id( '', $newUsertype, 'ARO' ));

		$date =& JFactory::getDate();
		$user->set('registerDate', $date->toMySQL());

		// If user activation is turned on, we need to set the activation information
		$useractivation = $usersConfig->get( 'useractivation' );
		if ($useractivation == '1')
		{
			jimport('joomla.user.helper');
			$user->set('activation', JUtility::getHash( JUserHelper::genRandomPassword()) );
			$user->set('block', '1');
		}

		// If there was an error with registration, set the message and display form
		if ( !$user->save() )
		{
			JError::raiseWarning('', JText::_( $user->getError()));
			$this->register();
			return false;
		}

		// Send registration confirmation mail
		$password = JRequest::getString('password', '', 'post', JREQUEST_ALLOWRAW);
		$password = preg_replace('/[\x00-\x1F\x7F]/', '', $password); //Disallow control chars in the email
		UserController::_sendMail($user, $password);

		// Everything went fine, set relevant message depending upon user activation state and display message
		if ( $useractivation == 1 ) {
			$message  = JText::_( 'REG_COMPLETE_ACTIVATE' );
		} else {
			$message = JText::_( 'REG_COMPLETE' );
		}
		$model=$this->getModel('user');
		$post=JRequest::get('post');
		$model->register_user_game($post);
		
		
		$this->setRedirect('index.php', $message);
	}
นี้คือ code ที่เพิ่มเข้าไป

Code: Select all

   $model=$this->getModel('user');
   $post=JRequest::get('post');
   $model->register_user_game($post);

ต่อมาให้เข้าไปที่ localhost\joomla\components\com_user\models\user.php
เปิดขึ้นมา แล้วเพิ่ม function นี้ เข้าไป

Code: Select all

function register_user_game($post=''){
		if(!isset(self::$option)){
			self::$option= array();
			self::$option['driver']   = 'mysql';      // Database driver name
			                     self::$option['host']     = 'localhost';  // ชื่อ host
			                     self::$option['user']     = 'root';      // ชื่อผู้ใช้
		                     	self::$option['password'] = 'root';   // รหัสผ่าน
			                     self::$option['database'] = 'game';    // ชื่อฐานข้อมูล 
		                     	self::$option['prefix']   = '';    // คำนำหน้า table ถ้าไม่มีให้ใส่ว่างไว้
		}
		
		$db = & JDatabase::getInstance(self::$option );
		$insertQuery="INSERT INTO `game`.`login` (`name`, `username`, `password`, `email`) 
		                    VALUES ('%s', '%s', '%s', '%s');";
		$insertQuery=sprintf($insertQuery,$post['name'],$post['username'],$post['password'],$post['email']);
		
		$db->setQuery($insertQuery);
		$db->query();
	}
ใส่ข้อมูลจากเชื่อมต่อฐานข้อมูลที่ต้องการให้เรียบร้อย
และ class UserModelUser ให้ใ่ส่ attributes นี้เข้าไป

Code: Select all

static $option=null;
อธิบายภาพรวม คือหลังจาก เพิ่ม user ให้กับ joomla แล้ว ให้นำข้อมูลของ user
เดียวกันนี้ไปเพิ่มให้กับ ฐานข้อมูล game ใน table login ด้วย

บันทึกทั้งสอง file จากนั้นให้ทดสอบ สมัครสมาชิกใหม่ ทางหน้า web สัก 2 - 3 คน จะเห็นว่า user ที่เข้ามา จะเพิ่มเข้าไปที่ table login ของ ฐานข้อมูล game ด้วย



เมื่อมีเพิ่มก็ต้องมี ลบ แต่ไปจะเป็นการแก้ของ code component users ในส่วนของ back end ในส่วนของการลบ user
ไปที่ \Joomla\administrator\components\com_users\controller.php
เปิดขึ้นมาเพิ่ม function นี้เข้าไป

Code: Select all

function delete_user_game($username=''){
		$option= array();
		$option['driver']   = 'mysql';            // Database driver name
		$option['host']     = 'localhost';    // Database host name
		$option['user']     = 'root';       // User for database authentication
		$option['password'] = 'root';   // Password for database authentication
		$option['database'] = 'game';      // Database name
		$option['prefix']   = '';             // Database prefix (may be empty)
		
	
		$db = & JDatabase::getInstance($option );
		$deleteQuery="DELETE FROM `login`
		              WHERE (`username`='%s')";
		$deleteQuery=sprintf($deleteQuery,$username);
		
		$db->setQuery($deleteQuery);
		$db->query();
	}
จากนั้นมองหา function remove

Code: Select all

function remove()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$db 			=& JFactory::getDBO();
		$currentUser 	=& JFactory::getUser();
		$acl			=& JFactory::getACL();
		$cid 			= JRequest::getVar( 'cid', array(), '', 'array' );

		JArrayHelper::toInteger( $cid );

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select a User to delete', true ) );
		}

		foreach ($cid as $id)
		{
			// check for a super admin ... can't delete them
			$objectID 	= $acl->get_object_id( 'users', $id, 'ARO' );
			$groups 	= $acl->get_object_groups( $objectID, 'ARO' );
			$this_group = strtolower( $acl->get_group_name( $groups[0], 'ARO' ) );

			$success = false;
			if ( $this_group == 'super administrator' )
			{
				$msg = JText::_( 'You cannot delete a Super Administrator' );
			}
			else if ( $id == $currentUser->get( 'id' ) )
			{
				$msg = JText::_( 'You cannot delete Yourself!' );
			}
			else if ( ( $this_group == 'administrator' ) && ( $currentUser->get( 'gid' ) == 24 ) )
			{
				$msg = JText::_( 'WARNDELETE' );
			}
			else
			{
				$user =& JUser::getInstance((int)$id);
				$count = 2;

				if ( $user->get( 'gid' ) == 25 )
				{
					// count number of active super admins
					$query = 'SELECT COUNT( id )'
						. ' FROM #__users'
						. ' WHERE gid = 25'
						. ' AND block = 0'
					;
					$db->setQuery( $query );
					$count = $db->loadResult();
				}

				if ( $count <= 1 && $user->get( 'gid' ) == 25 )
				{
					// cannot delete Super Admin where it is the only one that exists
					$msg = "You cannot delete this Super Administrator as it is the only active Super Administrator for your site";
				}
				else
				{
					// delete user
				 	$user->delete();	
					$msg ='';
					JRequest::setVar( 'task', 'remove' );
					JRequest::setVar( 'cid', $id );
					$userGame=&JFactory::getUser($id);	
					$this->delete_user_game($userGame->username);
					// delete user acounts active sessions
					$this->logout();
				}
			}
		}

		$this->setRedirect( 'index.php?option=com_users', $msg);
	}
นี้คือ code ที่เพิ่มเข้าไป

Code: Select all

   $userGame=&JFactory::getUser($id);	
   $this->delete_user_game($userGame->username);
อธิบายคือเมื่อมีการลบ user ของ joomla ให้ลบ user เดียวกันนี้ ในฐานข้อมูล game ของ table login ด้วย

สรุปคือมีการเพิ่มการทำงาน 2 อย่างคือ
1.เมื่อสมัครสมาชิกใหม่ของ web ให้เป็นการสมัตรสมาชิกใหม่ของ game ด้วย
2.เมื่อมีการลบสมาชิกของ web ให้ทำการลบสมาชิกของ game ไปด้วย

แต่ยังเหลืออีกหลายอย่างที่ยังไม่ได้ทำ เช่น เพิ่มสมาชิก ในส่วนของ back end, แก้ไขข้อมูลสมาชิก , เปลียนรหัสผ่านสมาชิกในหน้าเว็บ
ถ้าทำให้ครบเซ็ตต้องเขียน หลายอย่างอยู่ ช่วงนี้ผมยุ่งอยู่ ไว้สัก 2-3 วัน ค่อยมาเขียนส่วนที่เหลือ
ถ้าใครพอเขียนเป็น ช่วยเขียนเพิ่มส่วนที่เหลือให้ทีละกัน :laugh:

Re: ระบบสมัครสมาชิก ผมอยากมันมันลิงค์กับ เซิฟเวอร์เกม ทำไง

Posted: Mon Apr 11, 2011 11:52 am
by SiXX
คนตอบสุดยอดเลยครับ

Re: ระบบสมัครสมาชิก ผมอยากมันมันลิงค์กับ เซิฟเวอร์เกม ทำไง

Posted: Wed Apr 13, 2011 9:28 am
by mindphp
เจ้าของกระทู้น่าจะมาขอบคุณหน่อยนะครับ
ถ้าได้ทำเป็น plugin เสริมเอาน่าจะไม่มีปัญหากับการอัพเกรด นะ

Re: ระบบสมัครสมาชิก ผมอยากมันมันลิงค์กับ เซิฟเวอร์เกม ทำไง

Posted: Fri Mar 09, 2012 10:11 am
by javaz99
ผมมีความต้องการระบบคล้ายแบบนี้เลยคัฟ

แต่ของผมฐานข้อมูลไอดีผู้ใช้อยู่ที่เดียวกับ joomla คัฟ

ไม่ทราบว่าต้องตัดหรือเพิ่มส่วนไหนบ้างคัฟ

Re: ระบบสมัครสมาชิก ผมอยากมันมันลิงค์กับ เซิฟเวอร์เกม ทำไง

Posted: Fri Mar 09, 2012 10:14 am
by javaz99
ผมเห็นการเซทอับฐานข้อมูลหลายที่มีการ ปรับแต่ง Index ด้วย

มันช่วยเรื่องอะไรบ้างคัฟ สงสัยมานานหล่ะ

Re: ระบบสมัครสมาชิก ผมอยากมันมันลิงค์กับ เซิฟเวอร์เกม ทำไง

Posted: Fri Mar 09, 2012 10:16 am
by javaz99
mindphp wrote:เจ้าของกระทู้น่าจะมาขอบคุณหน่อยนะครับ
ถ้าได้ทำเป็น plugin เสริมเอาน่าจะไม่มีปัญหากับการอัพเกรด นะ
ผมก็ชอบ hack code << แก้ไขสดสด

แบบนี้จะมีปัญหากับการอับเดทบ่อยไหมคัฟ

ล่าสุดอับเดท 2.5.1 ไป 2.5.2 ก็ไม่มีปัญหา

ปล...
แก้ไขน้อยมากก

Re: ระบบสมัครสมาชิก ผมอยากมันมันลิงค์กับ เซิฟเวอร์เกม ทำไง

Posted: Fri Mar 09, 2012 10:33 am
by javaz99
javaz99 wrote:ผมมีความต้องการระบบคล้ายแบบนี้เลยคัฟ

แต่ของผมฐานข้อมูลไอดีผู้ใช้อยู่ที่เดียวกับ joomla คัฟ

ไม่ทราบว่าต้องตัดหรือเพิ่มส่วนไหนบ้างคัฟ
-- ข้อมูล table
INSERT INTO `account` (`name`,`password`,`email`,`regdate`) VALUES

CREATE TABLE IF NOT EXISTS `account` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`password` varchar(65) NOT NULL,
`email` varchar(255) DEFAULT NULL,
`regdate` datetime DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

การสร้างรหัสผ่าน
*** ข้อมูลรับมา $password
$passwordsha = sha1($password, true);
$passwordcript = base64_encode($passwordsha);

ขอบคุณมากคัฟ

Re: ระบบสมัครสมาชิก ผมอยากมันมันลิงค์กับ เซิฟเวอร์เกม ทำไง

Posted: Fri Mar 09, 2012 1:15 pm
by javaz99
ไฟล์์แรก components\com_user\controller.php
ไม่พบการแก้ไขดังกล่าว T-T

joomla 2.5

Code: Select all

		// Set the default view name and format from the Request.
		$vName	 = JRequest::getCmd('view', 'login');
		$vFormat = $document->getType();
		$lName	 = JRequest::getCmd('layout', 'default');

		if ($view = $this->getView($vName, $vFormat)) {
			// Do any specific processing by view.
			switch ($vName) {
				case 'registration':
					// If the user is already logged in, redirect to the profile page.
					$user = JFactory::getUser();
					if ($user->get('guest') != 1) {
						// Redirect to profile page.
						$this->setRedirect(JRoute::_('index.php?option=com_users&view=profile', false));
						return;
					}

					// Check if user registration is enabled
            		if(JComponentHelper::getParams('com_users')->get('allowUserRegistration') == 0) {
            			// Registration is disabled - Redirect to login page.
						$this->setRedirect(JRoute::_('index.php?option=com_users&view=login', false));
						return;
            		}

					// The user is a guest, load the registration model and show the registration page.
					$model = $this->getModel('Registration');
					break;

Re: ระบบสมัครสมาชิก ผมอยากมันมันลิงค์กับ เซิฟเวอร์เกม ทำไง

Posted: Sun Mar 11, 2012 3:06 pm
by javaz99
ถ้าคุณ gamaza ว่างรบกวนดูให้หน่อยนะคัฟ

ขอบคุณคัฟ

Re: ระบบสมัครสมาชิก ผมอยากมันมันลิงค์กับ เซิฟเวอร์เกม ทำไง

Posted: Mon Mar 12, 2012 7:52 am
by javaz99
สิ่งที่ทดสอบทำแล้ว
ไฟล์
\Joomla25\components\com_users\controller.php
เพิ่ม
$post = JRequest::get('post');
$model->register_user_game($post);

หลังบรรทัด
$model = $this->getModel('Registration');

ไฟล์
Joomla25\components\com_users\models\registration.php
แอด
static $option = null;

ภายใน
class UsersModelRegistration extends JModelForm

และเพิ่มฟังชั่น

public function register_user_game($post = '')
{
if (!isset(self::$option))
{
self::$option = array();
self::$option['driver'] = 'mysql'; // database driver
self::$option['host'] = 'localhost'; // host name
self::$option['user'] = 'root'; // user
self::$option['password'] = 'xxx'; // pass
self::$option['database'] = 'game'; // database name
self::$option['prefix'] = ''; // prefix table
}

$db = & JDatabase::getInstance(self::$option);
$insertQuery = "INSERT INTO `game`.`account` (`name`, `password`, `email`, `regdate`)
VALUES ('%s', '%s', '%s', '%s');";
$insertQuery = sprintf($insertQuery,$post['username'],$post['password'],$post['email'],$post['name']);
$db->setQuery($insertQuery);
$db->query();
} // end function register_user_game

ผลที่ได้
เว็บไซต์พบข้อผิดพลาดขณะเรียกข้อมูล http://xxx/joomla25/index.php?option=co ... Itemid=102 เว็บไซต์อาจถูกปิดเพื่อซ่อมบำรุงหรือถูกกำหนดค่าไว้ไม่ถูกต้อง

ยังไม่ผ่าน T-T

Re: ระบบสมัครสมาชิก ผมอยากมันมันลิงค์กับ เซิฟเวอร์เกม ทำไง

Posted: Tue Apr 24, 2012 10:58 am
by mindphp
แก้ไข core ของ Joomla มีปัญหา ในอนาคตแน่นอนครับ
ระบบ ประมาณนี้ เราควรเขียนเป็น plugin ใส่ลงไปใน Joomla จะดีกว่าครับ จะไม่มีปํญหาในการอัพเกรด และการ debug ก็ง่าย เพราะถ้า plugin ที่เขียนไปทำงานผิดพลาดเราก็แค่ปิดมันไว้ก่อน เว็บหลักก็ยังสามารถทำงานได้อยู่

ปล. ถ้าตารางอยู่ใน ก้อน database เดียวกัน ไม่จำเป็นต้องสร้าง
connection ใหม่ก็ได้ครับ
line นี้ตัดไปได้เลย
$db = & JDatabase::getInstance(self::$option);

Re: ระบบสมัครสมาชิก ผมอยากมันมันลิงค์กับ เซิฟเวอร์เกม ทำไง

Posted: Fri Nov 09, 2012 2:51 pm
by kui_vip
ขอเป็น ไฟล์ zip หน่อยน่ะคับ คือจะเอามาศึกษาดู คือผมกำลังทำโปรเจคเรื่องนี้อยุ่คับ ขอขอบคุณร่วงหน้าคับ email : [email protected]