Php 代码点火器+;社区身份验证重定向链接自动插入
我刚刚安装了Codeigniter模块。我想知道当我想访问该站点时,如何访问以及为什么访问http://mylocalsite.dev/examples 它会自动将页面地址交换为:Php 代码点火器+;社区身份验证重定向链接自动插入,php,codeigniter,Php,Codeigniter,我刚刚安装了Codeigniter模块。我想知道当我想访问该站点时,如何访问以及为什么访问http://mylocalsite.dev/examples 它会自动将页面地址交换为: http://mylocalsite.dev/login?redirect=examples 我找不到做这件事的地方。下面,我将向您介绍我的全部示例: /** *社区身份验证-示例控制器 * *Community Auth是CodeIgniter 3的开源身份验证应用程序 * *@package-Communit
http://mylocalsite.dev/login?redirect=examples
我找不到做这件事的地方。下面,我将向您介绍我的全部示例:
/**
*社区身份验证-示例控制器
*
*Community Auth是CodeIgniter 3的开源身份验证应用程序
*
*@package-Community-Auth
*@作者罗伯特·戈蒂埃
*@版权所有(c)2011-2016,Robert B Gottier。(http://brianswebdesign.com/)
*@license-BSD-http://www.opensource.org/licenses/BSD-3-Clause
*@linkhttp://community-auth.com
*/
类示例扩展了MY_控制器
{
公共函数构造()
{
父项::_构造();
//强制SSL
//$this->force_ssl();
//始终加载表单和URL帮助程序(仅为方便起见)
$this->load->helper('url');
$this->load->helper('form');
}
// -----------------------------------------------------------------------
/**
*演示被重定向到登录。
*如果您已登录并请求此方法,
*您将看到消息,否则您将
*显示登录表单。一旦实现登录,
*您将被重定向回此方法。
*/
公共职能指数()
{
如果($this->require_角色('admin'))
{
echo$this->load->view('examples/page_header','',TRUE);
回显“您已登录!”;
echo$this->load->view('examples/page\u footer','',TRUE);
}
}
// -----------------------------------------------------------------------
/**
*显示验证用户是否登录的基本页面。
*如果用户已登录,菜单中将显示“注销”链接。
*如果他们没有登录,菜单中将显示“登录”链接。
*/
公共功能之家()
{
$this->is_logged_in();
echo$this->load->view('examples/page_header','',TRUE);
回音“欢迎回家”;
echo$this->load->view('examples/page\u footer','',TRUE);
}
// -----------------------------------------------------------------------
/**
*演示可选登录。
*请记住将“示例/可选登录测试”添加到
*config/authentication.php中的\u登录数组允许的\u页面。
*
*请注意,我们使用verify_min_level检查
*用户已登录。
*/
公共函数可选\u登录\u测试()
{
如果($this->verify_min_level(1))
{
$page_content='虽然不是必需的,但您已登录!';
}
elseif($this->tokens->match&&$this->optional_login())
{
//让社区身份验证处理登录尝试。。。
}
其他的
{
//请注意,参数设置为TRUE,将其指定为可选登录
$this->setup\u login\u表单(TRUE);
$page_content='您尚未登录,但仍可以看到此页面。;
//需要表格助手
$this->load->helper('form');
$page\u content.=$this->load->view('examples/login\u form','',TRUE);
}
echo$this->load->view('examples/page_header','',TRUE);
echo$page_内容;
echo$this->load->view('examples/page\u footer','',TRUE);
}
// -----------------------------------------------------------------------
/**
*在这里,我们只需验证用户是否已登录,但是
*未强制身份验证。身份验证的存在
*不为空的相关变量表示
*某人已登录。还显示了如何
*获取HTTP用户cookie的内容。
*/
公共函数简单_验证()
{
$this->is_logged_in();
echo$this->load->view('examples/page_header','',TRUE);
回声“”;
如果(!empty($this->auth_角色))
{
echo$this->auth_角色。'已登录!
用户ID为“.$this->auth\u User\u ID”。
验证级别为“.$this->Auth_level”。
用户名为“.$this->auth_Username;
如果($http\u user\u cookie\u contents=$this->input->cookie(配置项('http\u user\u cookie\u name'))
{
$http\u user\u cookie\u contents=unserialize($http\u user\u cookie\u contents);
回声'
';
打印(http\u用户\u cookie\u内容);
回声';
}
if(配置项('add_acl_query_to_auth_functions')&&&$this->acl)
{
回声'
';
打印($this->acl);
回声';
}
/**
*ACL使用不要求将ACL添加到身份验证变量。
*如果在身份验证期间未执行查询,
*acl_许可函数将查询数据库。
*/
如果($this->acl\u允许('general.secret\u action'))
{
echo“ACL权限授予操作!”;
}
}
其他的
{
echo“没有人登录”;
}
回声“”;
echo$this->load->view('examples/page\u footer','',TRUE);
}
// -----------------------------------------------------------------------
/**
*最简单的用户创建。您当然会
*拥有添加用户的界面,您甚至可以允许用户
*注册并创建自己的帐户。
*
*$user_数据数组中使用的密码需要满足
*以下默认强度要求:
*-长度必须至少为8个字符
*-长度必须小于72个字符
*-必须至少有一个数字
*-必须至少有一个小写字母
*-必须至少有一个大写字母
*-不能有任何空格、制表符或其他空白字符
*-不允许使用反斜杠、撇号或引号字符
*/
公共函数create_user()
{
//为您的用户自定义此阵列
$user\u数据=[
'用户名'=>'测试',
“passwd”=>“test”,
'电子邮件'=>'osemka59@gmail.com',
'auth_level'=>'9',//9如果您想登录@example
/**
* Community Auth - Examples Controller
*
* Community Auth is an open source authentication application for CodeIgniter 3
*
* @package Community Auth
* @author Robert B Gottier
* @copyright Copyright (c) 2011 - 2016, Robert B Gottier. (http://brianswebdesign.com/)
* @license BSD - http://www.opensource.org/licenses/BSD-3-Clause
* @link http://community-auth.com
*/
class Examples extends MY_Controller
{
public function __construct()
{
parent::__construct();
// Force SSL
//$this->force_ssl();
// Form and URL helpers always loaded (just for convenience)
$this->load->helper('url');
$this->load->helper('form');
}
// -----------------------------------------------------------------------
/**
* Demonstrate being redirected to login.
* If you are logged in and request this method,
* you'll see the message, otherwise you will be
* shown the login form. Once login is achieved,
* you will be redirected back to this method.
*/
public function index()
{
if( $this->require_role('admin') )
{
echo $this->load->view('examples/page_header', '', TRUE);
echo '<p>You are logged in!</p>';
echo $this->load->view('examples/page_footer', '', TRUE);
}
}
// -----------------------------------------------------------------------
/**
* A basic page that shows verification that the user is logged in or not.
* If the user is logged in, a link to "Logout" will be in the menu.
* If they are not logged in, a link to "Login" will be in the menu.
*/
public function home()
{
$this->is_logged_in();
echo $this->load->view('examples/page_header', '', TRUE);
echo '<p>Welcome Home</p>';
echo $this->load->view('examples/page_footer', '', TRUE);
}
// -----------------------------------------------------------------------
/**
* Demonstrate an optional login.
* Remember to add "examples/optional_login_test" to the
* allowed_pages_for_login array in config/authentication.php.
*
* Notice that we are using verify_min_level to check if
* a user is already logged in.
*/
public function optional_login_test()
{
if( $this->verify_min_level(1) )
{
$page_content = '<p>Although not required, you are logged in!</p>';
}
elseif( $this->tokens->match && $this->optional_login() )
{
// Let Community Auth handle the login attempt ...
}
else
{
// Notice parameter set to TRUE, which designates this as an optional login
$this->setup_login_form(TRUE);
$page_content = '<p>You are not logged in, but can still see this page.</p>';
// Form helper needed
$this->load->helper('form');
$page_content .= $this->load->view('examples/login_form', '', TRUE);
}
echo $this->load->view('examples/page_header', '', TRUE);
echo $page_content;
echo $this->load->view('examples/page_footer', '', TRUE);
}
// -----------------------------------------------------------------------
/**
* Here we simply verify if a user is logged in, but
* not enforcing authentication. The presence of auth
* related variables that are not empty indicates
* that somebody is logged in. Also showing how to
* get the contents of the HTTP user cookie.
*/
public function simple_verification()
{
$this->is_logged_in();
echo $this->load->view('examples/page_header', '', TRUE);
echo '<p>';
if( ! empty( $this->auth_role ) )
{
echo $this->auth_role . ' logged in!<br />
User ID is ' . $this->auth_user_id . '<br />
Auth level is ' . $this->auth_level . '<br />
Username is ' . $this->auth_username;
if( $http_user_cookie_contents = $this->input->cookie( config_item('http_user_cookie_name') ) )
{
$http_user_cookie_contents = unserialize( $http_user_cookie_contents );
echo '<br />
<pre>';
print_r( $http_user_cookie_contents );
echo '</pre>';
}
if( config_item('add_acl_query_to_auth_functions') && $this->acl )
{
echo '<br />
<pre>';
print_r( $this->acl );
echo '</pre>';
}
/**
* ACL usage doesn't require ACL be added to auth vars.
* If query not performed during authentication,
* the acl_permits function will query the DB.
*/
if( $this->acl_permits('general.secret_action') )
{
echo '<p>ACL permission grants action!</p>';
}
}
else
{
echo 'Nobody logged in.';
}
echo '</p>';
echo $this->load->view('examples/page_footer', '', TRUE);
}
// -----------------------------------------------------------------------
/**
* Most minimal user creation. You will of course make your
* own interface for adding users, and you may even let users
* register and create their own accounts.
*
* The password used in the $user_data array needs to meet the
* following default strength requirements:
* - Must be at least 8 characters long
* - Must be at less than 72 characters long
* - Must have at least one digit
* - Must have at least one lower case letter
* - Must have at least one upper case letter
* - Must not have any space, tab, or other whitespace characters
* - No backslash, apostrophe or quote chars are allowed
*/
public function create_user()
{
// Customize this array for your user
$user_data = [
'username' => 'test',
'passwd' => 'test',
'email' => 'osemka59@gmail.com',
'auth_level' => '9', // 9 if you want to login @ examples/index.
];
$this->is_logged_in();
echo $this->load->view('examples/page_header', '', TRUE);
// Load resources
$this->load->model('examples/examples_model');
$this->load->model('examples/validation_callables');
$this->load->library('form_validation');
$this->form_validation->set_data( $user_data );
$validation_rules = [
[
'field' => 'username',
'label' => 'username',
'rules' => 'max_length[12]|is_unique[' . config_item('user_table') . '.username]',
'errors' => [
'is_unique' => 'Username already in use.'
]
],
[
'field' => 'passwd',
'label' => 'passwd',
'rules' => [
'trim',
'required',
[
'_check_password_strength',
[ $this->validation_callables, '_check_password_strength' ]
]
],
'errors' => [
'required' => 'The password field is required.'
]
],
[
'field' => 'email',
'label' => 'email',
'rules' => 'trim|required|valid_email|is_unique[' . config_item('user_table') . '.email]',
'errors' => [
'is_unique' => 'Email address already in use.'
]
],
[
'field' => 'auth_level',
'label' => 'auth_level',
'rules' => 'required|integer|in_list[1,6,9]'
]
];
$this->form_validation->set_rules( $validation_rules );
if( $this->form_validation->run() )
{
$user_data['passwd'] = $this->authentication->hash_passwd($user_data['passwd']);
$user_data['user_id'] = $this->examples_model->get_unused_id();
$user_data['created_at'] = date('Y-m-d H:i:s');
// If username is not used, it must be entered into the record as NULL
if( empty( $user_data['username'] ) )
{
$user_data['username'] = NULL;
}
$this->db->set($user_data)
->insert(config_item('user_table'));
if( $this->db->affected_rows() == 1 )
echo '<h1>Congratulations</h1>' . '<p>User ' . $user_data['username'] . ' was created.</p>';
}
else
{
echo '<h1>User Creation Error(s)</h1>' . validation_errors();
}
echo $this->load->view('examples/page_footer', '', TRUE);
}
// -----------------------------------------------------------------------
/**
* This login method only serves to redirect a user to a
* location once they have successfully logged in. It does
* not attempt to confirm that the user has permission to
* be on the page they are being redirected to.
*/
public function login()
{
// Method should not be directly accessible
if( $this->uri->uri_string() == 'examples/login')
show_404();
if( strtolower( $_SERVER['REQUEST_METHOD'] ) == 'post' )
$this->require_min_level(1);
$this->setup_login_form();
$html = $this->load->view('examples/page_header', '', TRUE);
$html .= $this->load->view('examples/login_form', '', TRUE);
$html .= $this->load->view('examples/page_footer', '', TRUE);
echo $html;
}
// --------------------------------------------------------------
/**
* Log out
*/
public function logout()
{
$this->authentication->logout();
// Set redirect protocol
$redirect_protocol = USE_SSL ? 'https' : NULL;
redirect( site_url( LOGIN_PAGE . '?logout=1', $redirect_protocol ) );
}
// --------------------------------------------------------------
/**
* User recovery form
*/
public function recover()
{
// Load resources
$this->load->model('examples/examples_model');
/// If IP or posted email is on hold, display message
if( $on_hold = $this->authentication->current_hold_status( TRUE ) )
{
$view_data['disabled'] = 1;
}
else
{
// If the form post looks good
if( $this->tokens->match && $this->input->post('email') )
{
if( $user_data = $this->examples_model->get_recovery_data( $this->input->post('email') ) )
{
// Check if user is banned
if( $user_data->banned == '1' )
{
// Log an error if banned
$this->authentication->log_error( $this->input->post('email', TRUE ) );
// Show special message for banned user
$view_data['banned'] = 1;
}
else
{
/**
* Use the authentication libraries salt generator for a random string
* that will be hashed and stored as the password recovery key.
* Method is called 4 times for a 88 character string, and then
* trimmed to 72 characters
*/
$recovery_code = substr( $this->authentication->random_salt()
. $this->authentication->random_salt()
. $this->authentication->random_salt()
. $this->authentication->random_salt(), 0, 72 );
// Update user record with recovery code and time
$this->examples_model->update_user_raw_data(
$user_data->user_id,
[
'passwd_recovery_code' => $this->authentication->hash_passwd($recovery_code),
'passwd_recovery_date' => date('Y-m-d H:i:s')
]
);
// Set the link protocol
$link_protocol = USE_SSL ? 'https' : NULL;
// Set URI of link
$link_uri = 'examples/recovery_verification/' . $user_data->user_id . '/' . $recovery_code;
$view_data['special_link'] = anchor(
site_url( $link_uri, $link_protocol ),
site_url( $link_uri, $link_protocol ),
'target ="_blank"'
);
$view_data['confirmation'] = 1;
}
}
// There was no match, log an error, and display a message
else
{
// Log the error
$this->authentication->log_error( $this->input->post('email', TRUE ) );
$view_data['no_match'] = 1;
}
}
}
echo $this->load->view('examples/page_header', '', TRUE);
echo $this->load->view('examples/recover_form', ( isset( $view_data ) ) ? $view_data : '', TRUE );
echo $this->load->view('examples/page_footer', '', TRUE);
}
// --------------------------------------------------------------
/**
* Verification of a user by email for recovery
*
* @param int the user ID
* @param string the passwd recovery code
*/
public function recovery_verification( $user_id = '', $recovery_code = '' )
{
/// If IP is on hold, display message
if( $on_hold = $this->authentication->current_hold_status( TRUE ) )
{
$view_data['disabled'] = 1;
}
else
{
// Load resources
$this->load->model('examples/examples_model');
if(
/**
* Make sure that $user_id is a number and less
* than or equal to 10 characters long
*/
is_numeric( $user_id ) && strlen( $user_id ) <= 10 &&
/**
* Make sure that $recovery code is exactly 72 characters long
*/
strlen( $recovery_code ) == 72 &&
/**
* Try to get a hashed password recovery
* code and user salt for the user.
*/
$recovery_data = $this->examples_model->get_recovery_verification_data( $user_id ) )
{
/**
* Check that the recovery code from the
* email matches the hashed recovery code.
*/
if( $recovery_data->passwd_recovery_code == $this->authentication->check_passwd( $recovery_data->passwd_recovery_code, $recovery_code ) )
{
$view_data['user_id'] = $user_id;
$view_data['username'] = $recovery_data->username;
$view_data['recovery_code'] = $recovery_data->passwd_recovery_code;
}
// Link is bad so show message
else
{
$view_data['recovery_error'] = 1;
// Log an error
$this->authentication->log_error('');
}
}
// Link is bad so show message
else
{
$view_data['recovery_error'] = 1;
// Log an error
$this->authentication->log_error('');
}
/**
* If form submission is attempting to change password
*/
if( $this->tokens->match )
{
$this->examples_model->recovery_password_change();
}
}
echo $this->load->view('examples/page_header', '', TRUE);
echo $this->load->view( 'examples/choose_password_form', $view_data, TRUE );
echo $this->load->view('examples/page_footer', '', TRUE);
}
// --------------------------------------------------------------
/**
* Attempt to login via AJAX
*/
public function ajax_login()
{
$this->is_logged_in();
$this->tokens->name = 'login_token';
$data['javascripts'] = [
'https://code.jquery.com/jquery-1.12.0.min.js'
];
if( $this->authentication->on_hold === TRUE )
{
$data['on_hold_message'] = 1;
}
// This check for on hold is for normal login attempts
else if( $on_hold = $this->authentication->current_hold_status() )
{
$data['on_hold_message'] = 1;
}
$data['final_head'] = "<script>
$(document).ready(function(){
$(document).on( 'submit', 'form', function(e){
$.ajax({
type: 'post',
cache: false,
url: '/examples/ajax_attempt_login',
data: {
'login_string': $('#login_string').val(),
'login_pass': $('#login_pass').val(),
'login_token': $('[name=\"login_token\"]').val()
},
dataType: 'json',
success: function(response){
$('[name=\"login_token\"]').val( response.token );
console.log(response);
if(response.status == 1){
$('form').replaceWith('<p>You are now logged in.</p>');
$('#login-link').attr('href','/examples/logout').text('Logout');
$('#ajax-login-link').parent().hide();
}else if(response.status == 0 && response.on_hold){
$('form').hide();
$('#on-hold-message').show();
alert('You have exceeded the maximum number of login attempts.');
}else if(response.status == 0 && response.count){
alert('Failed login attempt ' + response.count + ' of ' + $('#max_allowed_attempts').val());
}
}
});
return false;
});
});
</script>";
$html = $this->load->view('examples/page_header', $data, TRUE);
$html .= $this->load->view('examples/ajax_login_form', $data, TRUE);
$html .= $this->load->view('examples/page_footer', '', TRUE);
echo $html;
}
// --------------------------------------------------------------
/**
* Test for login via ajax
*/
public function ajax_attempt_login()
{
if( $this->input->is_ajax_request() )
{
// Allow this page to be an accepted login page
$this->config->set_item('allowed_pages_for_login', ['examples/ajax_attempt_login'] );
// Make sure we aren't redirecting after a successful login
$this->authentication->redirect_after_login = FALSE;
// Do the login attempt
$this->auth_data = $this->authentication->user_status( 0 );
// Set user variables if successful login
if( $this->auth_data )
$this->_set_user_variables();
// Call the post auth hook
$this->post_auth_hook();
// Login attempt was successful
if( $this->auth_data )
{
echo json_encode([
'status' => 1,
'user_id' => $this->auth_user_id,
'username' => $this->auth_username,
'level' => $this->auth_level,
'role' => $this->auth_role,
'email' => $this->auth_email
]);
}
// Login attempt not successful
else
{
$this->tokens->name = 'login_token';
$on_hold = (
$this->authentication->on_hold === TRUE OR
$this->authentication->current_hold_status()
)
? 1 : 0;
echo json_encode([
'status' => 0,
'count' => $this->authentication->login_errors_count,
'on_hold' => $on_hold,
'token' => $this->tokens->token()
]);
}
}
// Show 404 if not AJAX
else
{
show_404();
}
}
// -----------------------------------------------------------------------
/**
* If you are using some other way to authenticate a created user,
* such as Facebook, Twitter, etc., you will simply call the user's
* record from the database, and pass it to the maintain_state method.
*
* So, you must know either the user's username or email address to
* log them in.
*
* How you would safely implement this in your application is your choice.
* Please keep in mind that such functionality bypasses all of the
* checks that Community Auth does during a normal login.
*/
public function social_login()
{
// Add the username or email address of the user you want logged in:
$username_or_email_address = '';
if( ! empty( $username_or_email_address ) )
{
$auth_model = $this->authentication->auth_model;
// Get normal authentication data using username or email address
if( $auth_data = $this->{$auth_model}->get_auth_data( $username_or_email_address ) )
{
/**
* If redirect param exists, user redirected there.
* This is entirely optional, and can be removed if
* no redirect is desired.
*/
$this->authentication->redirect_after_login();
// Set auth related session / cookies
$this->authentication->maintain_state( $auth_data );
}
}
else
{
echo 'Example requires that you set a username or email address.';
}
}
// -----------------------------------------------------------------------
}
/* End of file Examples.php */
/*
Location: /community_auth/controllers/Examples.php */