Difference between revisions of "Code Igniter"

From KOP KB
Jump to: navigation, search
(Google Recpatcha with Ion Auth)
(Verification Message and Error Catching)
Line 48: Line 48:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
==== Verification Message and Error Catching ====
 
==== Verification Message and Error Catching ====
 +
This is the entire Login function within the controllers for the ion auth plugin. If you compare you will notice. Location within application folder: controllers/Auth.php
 
<syntaxhighlight lang="php">
 
<syntaxhighlight lang="php">
 
function login()
 
function login()

Revision as of 16:43, 2 April 2016

Route Related

Forcing traffic to specific controller and method

Pretty much what were trying to do is make this url:
https://www.domain.com/home/pages/about
Become This one:
https://www.domain.com/about

In the routes file below put in:

$route['default_controller'] = 'home/pages';
$route['(:any)'] = 'home/pages';

Source: http://stackoverflow.com/questions/30338589/codeigniter-force-all-traffic-to-specific-controller-and-method

Auth Related

Ion-Auth redirect back to original page

Somone goes to said controller and needs to login to it the get forced to the auth page:

example.com/index.php/dashboard

To make it go back there you need to put the following in the auth controller were you see comments to that says login sucessfully

redirect($this->session->userdata('refered_from'),"refresh");

Source: http://stackoverflow.com/questions/18903315/redirect-to-previous-page-after-login-ion-auth-codeigniter

Checking Certain parts about someone being logged in

Simple syntax to check whether someon is logged in using Ion Auth.

if($this->ion_auth->logged_in()) //they are logged in
if(!$this->ion_auth->logged_in()) // not logged in
if($this->ion_auth->is_admin()) // They are logged in as admin
if($this->ion_auth->in_group("groupname"))// They are logged in to specified group

Source: John S.

Google Recpatcha with Ion Auth

Header Script

Put this in the header of auth login page. Filename from appplication: views/Auth/Login.php

<script src='https://www.google.com/recaptcha/api.js'></script>

The Captcha =

Put this as near to the submit button as you can. That's mostly my preference but depending on your style put it where you need. Also there is a theme that you can choose. If you just want the regular version just take out the data-theme attribute.

<div class="g-recaptcha" data-sitekey="yourpublickey" data-theme="dark" ></div>

Verification Message and Error Catching

This is the entire Login function within the controllers for the ion auth plugin. If you compare you will notice. Location within application folder: controllers/Auth.php

function login()
	{
		$this->data['title'] = "Login";
		
		
		
		//validate form input
		$this->form_validation->set_rules('identity', 'Identity', 'required');
		$this->form_validation->set_rules('password', 'Password', 'required');

		if ($this->form_validation->run() == true)
		{
			//check to see if the user is logging in
			//check for "remember me"
			$remember = (bool) $this->input->post('remember');
			$secret="6Lf9G_USAAAAAAyZN0SrVZBW3s7e0zoBi7ecbp6c";
$response=$_POST["g-recaptcha-response"];
$verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
$captcha_success=json_decode($verify);

if ($captcha_success->success==false) {
		$this->session->set_flashdata('message', "<p style='color:#220000;'><b><i>Please fill out captcha.</i></b></p>");
		redirect('auth/login', 'refresh');
}
else if ($captcha_success->success==true) {
	
    // captcha and let login
    	if ($this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember))
			{
				//if the login is successful
				//redirect them back to the home page
				$this->session->set_flashdata('message', $this->ion_auth->messages());
				if($this->ion_auth->is_admin()){
					
					redirect('auth/index',"refresh");
				
				}else{
					redirect('home',"refresh");
				}
			}
			else
			{
				//if the login was un-successful
				//redirect them back to the login page
				$this->session->set_flashdata('message', $this->ion_auth->errors());
				redirect('auth/login', 'refresh'); //use redirects instead of loading views for compatibility with MY_Controller libraries
			}
}
		
		}
		else
		{
			//the user is not logging in so display the login page
			//set the flash data error message if there is one
			$this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
			$this->data['jsli'] = "0";
			$this->data['identity'] = array('name' => 'identity',
				'id' => 'identity',
				'type' => 'text',
				'value' => $this->form_validation->set_value('identity'),
			);
			$this->data['password'] = array('name' => 'password',
				'id' => 'password',
				'type' => 'password',
			);

			$this->_render_page('auth/login', $this->data);
			
		}
	}

Error Related

Making Sure your errors get properly styled

I am not sure how this will apply to other people but pretty much is at the top of all my error screens so that the css can always be called correctly. Granted its an error page but make your errors look god damn fantastic.

defined('BASEPATH') OR exit('No direct script access allowed');
// get the path to where you are
$url = $_SERVER['REQUEST_URI'];
//count the forward slashes
$count = count_chars($url, 1);
$urlcount = "";
//Take what you counted and make the path
// 47 I believe is the ascii code for / so therefore it counts those to determine how far down
for ($x = 1; $x < $count[47]; $x++) {
    $urlcount = "../" . $urlcount;
}
//More or less this if and else statement is for the top directory and changes the string as necessary
if ($urlcount != null){
	
}else{
	$urlcount="./";
}

Only variable references should be returned by reference

Pretty much this is an issue with earlier versions of Code Igniter so for some this might not even be an issue. But basically it shows up below between lines 200 and 300 as the code below:

return $_config[0] =& $config;

And below this is what it needs to be changed to:

$_config[0] =& $config;
return $_config[0];

SOURCE: http://stackoverflow.com/questions/28348879/only-variable-references-should-be-returned-by-reference-codeigniter#