Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > PHP > PHP Forum > PHP Login 2 ตาราง ระหว่าง Admin กับ User สามารถทำได้ไหม



 

PHP Login 2 ตาราง ระหว่าง Admin กับ User สามารถทำได้ไหม

 



Topic : 123582



โพสกระทู้ ( 27 )
บทความ ( 0 )



สถานะออฟไลน์




สอบถามครับ ถ้าเราจะ Login 2 ตารางในรูปแบบ php สามารถทำได้ไหมครับ
แบบว่า ของ User ตารางหนึ่ง ของ Admin ตารางหนึ่ง แต่ Admin สามารถเข้าไปจัดการกับ User ได้ทุกอย่างเสมือนกับว่าอยู่ในตารางเดียวกัน
แบบนี้พอจะมีท่านใดช่วยแนะนำและมีโค๊ดให้ดูเป็นตัวอย่างบ้างไหมครับ พอดีทำโปรเจคๆหนึ่งแต่ไม่รู้ว่าจะเริ่มอย่างไรกับการทำ 2 ตารางนี้ครับ



Tag : PHP









ประวัติการแก้ไข
2016-06-28 00:26:28
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2016-06-28 00:25:51 By : nakorntim View : 1755 Reply : 4
 

 

No. 1

Guest


ทำได้สิ ก็แยก provider เอา

config\auth.php
<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Authentication Defaults
    |--------------------------------------------------------------------------
    |
    | This option controls the default authentication "guard" and password
    | reset options for your application. You may change these defaults
    | as required, but they're a perfect start for most applications.
    |
    */

    'defaults' => [
        'guard' => 'users',
        'passwords' => 'users',
    ],

    /*
    |--------------------------------------------------------------------------
    | Authentication Guards
    |--------------------------------------------------------------------------
    |
    | Next, you may define every authentication guard for your application.
    | Of course, a great default configuration has been defined for you
    | here which uses session storage and the Eloquent user provider.
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | Supported: "session", "token"
    |
    */

    'guards' => [
        'users' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'admins' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | User Providers
    |--------------------------------------------------------------------------
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | If you have multiple user tables or models you may configure multiple
    | sources which represent each model / table. These sources may then
    | be assigned to any extra authentication guards you have defined.
    |
    | Supported: "database", "eloquent"
    |
    */

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
        'admins' => [
            'driver' => 'eloquent',
            'model' => App\Administrator::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Resetting Passwords
    |--------------------------------------------------------------------------
    |
    | Here you may set the options for resetting passwords including the view
    | that is your password reset e-mail. You may also set the name of the
    | table that maintains all of the reset tokens for your application.
    |
    | You may specify multiple password reset configurations if you have more
    | than one user table or model in the application and you want to have
    | separate password reset settings based on the specific user types.
    |
    | The expire time is the number of minutes that the reset token should be
    | considered valid. This security feature keeps tokens short-lived so
    | they have less time to be guessed. You may change this as needed.
    |
    */

    'passwords' => [
        'users' => [
            'provider' => 'users',
            'email' => 'website.emails.reset',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],

];


app\Http\Controllers\Admin\AuthController.php
<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Admin;
use Statistic;
use Auth;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    /**
     * Where to redirect administartors after login / registration.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Only administartor authorize to access this section.
     *
     * @var string
     */
    protected $guard = 'admins';

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware($this->guestMiddleware(), ['except' => 'getLogout']);
    }

    /**
     * Assign view for login form.
     *
     * @var string
     */
    protected $loginView = 'admin.auth.login';

    /**
     * Handle an authentication attempt.
     *
     * @return Response
     */
    public function postLogin(Request $request) {
        // grab credentials from the request
        $credentials = $request->only('email', 'password');
        $remember = $request->has('remember') ? true : false;

        $rules = [
            'email' => 'required|email|exists:administrators,email,deleted_at,NULL',
            'password' => 'required|min:6'
        ];

        $validator = Validator::make($credentials, $rules);

        if ($validator->fails()) {
            return redirect()->back()
                ->withErrors($validator)
                ->withInput($request->except('password'));
        }
        else {
            if (Auth::guard($this->guard)->attempt($credentials, $remember)) {
                Statistic::administartor(Auth::guard($this->guard)->id());

                return redirect()->route('admin.index');
            }
            else {
                return redirect()->back()
                    ->withErrors(trans('auth.failed'))
                    ->withInput($request->except('password'));
            }
        }
    }
}


app\Http\Controllers\Website\AuthController.php
<?php

namespace App\Http\Controllers\Website;

use Illuminate\Http\Request;

use App\User;
use App\Member;
use Statistic;
use Auth;
use DB;
use Mail;

use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    /**
     * Where to redirect users after login / registration.
     *
     * @var string
     */
    protected $redirectTo = '/member';

    /**
     * Where to redirect users after logout.
     *
     * @var string
     */
    protected $redirectAfterLogout = '/auth/login';

    /**
     * Only user authorize to access this section.
     *
     * @var string
     */
    protected $guard = 'users';

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware($this->guestMiddleware(), ['except' => 'getLogout']);
    }

    /**
     * Assign view for login form.
     *
     * @var string
     */
    protected $loginView = 'website.auth.login';

    /**
     * Handle an authentication attempt.
     *
     * @return Response
     */
    public function postLogin(Request $request) {
        // grab credentials from the request
        $credentials = $request->only('email', 'password');

        $rules = [
            'email' => 'required|email|exists:users,email,deleted_at,NULL',
            'password' => 'required|min:6'
        ];

        $attributeNames = [
            'email' => 'อีเมล',
            'password' => 'รหัสผ่าน',
        ];

        $validator = Validator::make($credentials, $rules);
        $validator->setAttributeNames($attributeNames);

        $validator->after(function($validator) use ($request) {
            $user = User::where('email', $request->input('email'))->first();

            if (!is_null($user)) {
                if (!$user->confirmed) {
                    $validator->errors()->add('verify', 'ยังไม่ได้ทำการยืนยันข้อมูลสมาชิกนี้ โปรดตรวจสอบอีเมลที่ได้รับจากระบบ');
                }
            }
        });

        if ($validator->fails()) {
            return redirect()->back()
                ->withErrors($validator)
                ->withInput($request->except('password'));
        }
        else {
            if (Auth::guard($this->guard)->attempt($credentials, $request->has('remember'))) {
                Statistic::user(Auth::guard($this->guard)->id());

                return redirect()->route('website.member.index');
            }
            else {
                return redirect()->back()
                    ->withErrors(trans('auth.failed'))
                    ->withInput($request->except('password'));
            }
        }
    }

    /**
     * Assign view for register form.
     *
     * @var string
     */
    protected $registerView = 'website.auth.register';

    /**
     * Handle an user registation.
     *
     * @return Response
     */
    public function postRegister(Request $request) {
        // grab inputs from the request
        $register = $request->except('terms');

        $rules = [
            'email' => 'required|email|max:255|unique:users,email',
            'password' => 'required|min:6|confirmed',
            'citizen_code' => 'required|min:13|exists:profiles,citizen_code,deleted_at,NULL',
            'member_id' => 'required|exists:members,id,leave_date,NULL,deleted_at,NULL|unique:users,member_id',
        ];

        $attributeNames = [
            'email' => 'อีเมล',
            'password' => 'รหัสผ่าน',
            'citizen_code' => 'เลขประจำตัวประชาชน',
            'member_id' => 'รหัสสมาชิก',
        ];

        $validator = Validator::make($register, $rules);
        $validator->setAttributeNames($attributeNames);

        $validator->after(function($validator) use ($request) {
            $member = Member::find($request->input('member_id'));

            if (!is_null($member)) {
                if ($member->profile->citizen_code != $request->input('citizen_code')) {
                    $validator->errors()->add('citizen_code_notmatch', 'ข้อมูล เลขประจำตัวประชาชน ไม่ตรงกับข้อมูลสมาชิก');
                }
            }
        });

        if ($validator->fails()) {
            return redirect()->back()
                ->withErrors($validator)
                ->withInput($request->except(['password', 'member_id', 'terms']));
        }
        else {
            DB::transaction(function() use ($request) {
                $user = new User($request->only('email', 'password'));
                $member = Member::find($request->input('member_id'));
                $member->user()->save($user);

                $token = hash_hmac('sha256', str_random(40), config('app.key'));
                $confirm = DB::table('user_confirmations')->insert([
                        'email' => $request->input('email'), 
                        'token' => $token
                    ]);

                Mail::send('website.emails.verify', ['token' => $token], function($message) use ($user) {
                    $message->to($user->email, $user->member->profile->name . " " . $user->member->profile->lastname)
                        ->subject('Please Verify Your Email Address.');
                });
            });

            return redirect()->back()
                ->with('registed', 'ลงทะเบียนเรียบร้อยแล้ว คุณต้องเข้ายืนยันการใช้งานจากลิงก์ที่ส่งไปยังอีเมล ' . $request->input('email'));
        }
    }

    /**
     * Responds to requests to GET /auth/verify/SeMXnmSNLPzcQvWFnoTGdmj4OucAfe2UpbbrBu28HdY=
     */
    public function getVerify($token) {
        if(!$token) {
            return redirect()->route('website.index');
        }

        $confirm = DB::table('user_confirmations')
            ->where('token', $token)
            ->first();

        if (!$confirm) {
            return redirect()->route('website.index');
        }

        DB::transaction(function() use ($confirm) {
            $user = User::where('email', $confirm->email)->first();
            $user->forceFill(['confirmed' => true])->save();

            DB::table('user_confirmations')
                ->where('token', $confirm->token)
                ->delete();
        });

        return redirect()->route('website.auth.login')
            ->with('verified', 'คุณทำการยืนยันอีเมลเรียบร้อยแล้ว')
            ->withInput($confirm->email);
    }
}







แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-06-28 07:53:22 By : ห้ามตอบเกินวันละ 2 กระทู้
 


 

No. 2



โพสกระทู้ ( 66 )
บทความ ( 0 )



สถานะออฟไลน์


ไม่รู้เราเข้าใจถูกมั้ย

ปกติ user เก็บเป็นตารางเดียวแต่จะมี field นึงไว้แบบประเภท user เช่น
level = 1 (developer)
level = 2 (admin)
level = 3 (user)
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-06-28 09:33:14 By : tomguitar
 

 

No. 3

Guest


มันก็แล้วแต่กรณีนะ อย่างของเราที่ยกตัวอย่าง มันจะแยกด้วย sub domain

www.xxxxx.com (หน้าบ้าน) -> ใช้ user auth

admin.xxxx.com (หลังบ้าน) -> ใช้ admin auth

โดย default guard จะเป็น user

ส่วน admin ต้องกำหนด guard เป็น admin ถึงเรียกใช้ auth ของ admin ได้
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-06-28 09:55:40 By : ห้ามตอบเกินวันละ 2 กระทู้
 


 

No. 4



โพสกระทู้ ( 4,169 )
บทความ ( 7 )

Hall of Fame 2012

สถานะออฟไลน์


ตอบความคิดเห็นที่ : 1 เขียนโดย : ห้ามตอบเกินวันละ 2 กระทู้ เมื่อวันที่ 2016-06-28 07:53:22
รายละเอียดของการตอบ ::
เล่นจัดเต็มมาเลยนะพ่อคุ้น

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-06-28 10:14:01 By : dudesaranyu
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : PHP Login 2 ตาราง ระหว่าง Admin กับ User สามารถทำได้ไหม
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 05
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่