Trying to build the simplest codeigniter captcha according to the codeignuter help instructions but it dosen't work - codeigniter

Trying to build the simplest codeigniter captcha according to the codeigniter help instructions but it dosen't work.
My controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class c_captche extends CI_Controller {
//http://stackoverflow.com/questions/9484480/couldnt-connect-to-helper-in-codeigniter
//https://www.youtube.com/watch?v=TU_8b9SRe_k
//https://www.youtube.com/watch?v=tAmAxdSGZSs
/*
http://ellislab.com/codeigniter/user-guide/helpers/captcha_helper.html
https://www.youtube.com/watch?v=RR3ODc0vDvA
http://only4ututorials.blogspot.in/2014/04/how-to-create-captcha-with-codeigniter.html
http://www.cecilieo.com/techblog/how-to-use-codeigniter-captcha-plug-in/
*/
public function __construct()
{
parent::__construct();
$this->load->library('image_lib') ;
//$this->load->helper(array('captche'));
$this->load->helper('captcha');
}
public function index() {
echo "in c_captche";
$data = array(
'word' => 'Random word',
'img_path' => './captcha/',
'img_url' => base_url().'application/captcha/',
'img_width' => '150',
'img_height' => 30,
'expiration' => 7200
);
$captch=create_captcha( $data);
print_r($captch);
$this->load->view('v_captche',$captch);
}
}
My view:
<?php
print_r($captch);
echo $captch ;
?>
I created folder called captcha under application folder.
No image been created there.
The error msg i get is:
PHP Error was encountered
Severity: Notice
Message: Undefined variable: captch
Filename: views/v_captche.php
Line Number: 8
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: captcha
Filename: views/v_captche.php
Line Number: 9
But even before the error msg, no img is been created in the folder.

As said in the documentation :
The "captcha" folder must be writable (666, or 777)
So check your folder rights, and you have to create this folder at your index.php root.
Then, for your view, you should use :
$this->load->view('v_captche',array('captcha' => $captch));
And in your view :
print_r($captcha);
echo $captcha['image'];

Related

How to load model from hooks file in ci3 [duplicate]

This question already has answers here:
How do I load my models inside a codeigniter hook file
(2 answers)
Closed 5 months ago.
I'm trying to learn how to use hooks in ci3, is it possible to load a model from hooks file in CI3? because when I try to load the model it gives me this error:
A PHP Error was encountered
Severity: Warning
Message: Undefined property: Http_request_logger::$load
Filename: hooks/http_request_logger.php
Line Number: 9
And
An uncaught Exception was encountered
Type: Error
Message: Call to a member function model() on null
Filename: D:\xampp\htdocs\vobot-cms\application\hooks\http_request_logger.php
Line Number: 9
and here's my hooks code:
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Http_request_logger {
public function activity_user() {
$CI = & get_instance();
$this->load->model('./activity_user/MActivityUser');
// log_message('info', 'GET --> ' . var_export($CI->input->get(null), true));
$post = log_message('info', 'POST --> ' . var_export($CI->input->post(null), true));
// log_message('info', '$_SERVER -->' . var_export($_SERVER, true));
$this->MActivityUser->insertLogActivity( $data = array(
'username' => (!$this->MActivityUser->getUsername() ? NULL : $this->MActivityUser->getUsername()),
// 'domain' => $_SERVER["SERVER_NAME"],
'menu_name' => $_SERVER["REQUEST_URI"],
'activity' => $post,
// 'ip' => $this->CI->input->ip_address(),
'create_date' => date('Y-m-d H:i:s'),
'cms_id' => $this->MActivityUser->getUserId()
));
return $data;
}
}
The error is that you are loading the CI instance into the $CI variable but you are not using it to load your model.
$CI = & get_instance();
$this->load->model('./activity_user/MActivityUser');
should be
$CI = & get_instance();
$CI->load->model('./activity_user/MActivityUser');

Codeigniter redirect method is not working

This is my User.php controller
I am unable to use redirect method.
i am working on xampp localhost
?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class User extends CI_Controller {
public function __construct()
{
parent::__construct();
// Your own constructor code
$this->load->library('Admin_layout');
$this->config->load('reg_rules');
$this->load->model('admin/user_model');
$this->load->helper('form');
$this->load->helper('url');
}
public function index()
{
if (!$this->auth->loggedin()) {
redirect('admin/login');
}
}
public function add(){
//if($this->input->post('submit')){
$this->form_validation->set_rules($this->config->item('reg_settings'));
$data["reg_attrib"] = $this->config->item("reg_attribute");
$this->form_validation->set_error_delimiters('', '');
if ($this->form_validation->run('submit') == FALSE)
{
// templating
$this->admin_layout->set_title('Add a User');
$this->admin_layout->view('admin/add_user',$data["reg_attrib"]);
// templating
}
else
{
// Develop the array of post data and send to the model.
$passw = $this->input->post('password');
$hashpassword = $this->hash($passw);
$user_data = array(
'name' => $this->input->post('name'),
'gender' => $this->input->post('gender'),
'phone' => $this->input->post('contact_no'),
'email' => $this->input->post('email'),
'password' => $this->hash($hashpassword),
'doj' => time(),
);
$user_id = $this->user_model->create_user($user_data);
Here i am setting my success message using set_flashdata
and redirecting
if($user_id){
$this->session->set_flashdata('item', 'Record created successfully');
$this->redirect('admin/user/add','refresh');
}else{
echo "User Registration Failed!";
}
}//else
//} // submit
} // add
}
View_users.php
<?php
if($this->session->flashdata('item'))
{
echo $message = $this->session->flashdata('item');
}
?>
I am getting the following error
Fatal error: Call to undefined method User::redirect() in C:\xampp\htdocs\ci\application\controllers\admin\User.php on line 67
A PHP Error was encountered
Severity: Error
Message: Call to undefined method User::redirect()
Filename: admin/User.php
Line Number: 67
Backtrace:
Try to change from
$this->redirect('admin/user/add','refresh');
to
redirect('admin/user/add','refresh');
Hope it will be useful for you.

How to write component in codeigniter

I want to know how can I write a component in codeigniter
I have worked with symfony1.4 and there there is something include_component("name",dataarray()) that we can load a component ( it's actually an action ).
I already know that we have $this->load->view('admin/template/login') for loading a view, But I want to know is there any way to call an action like this?
this->load(news/list,array('date'=>xxx-xx-xx))
thanks anyways
you can this through the use of libraries, helpers or plugins.
an example of using a library class in application/library called LoadNews.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class LoadNews {
public function showList($date_array) {
// do stuff with date array
}
}
/* End of file LoadNews.php */
/* Location: ./application/libraries/LoadNews.php */
then in your controller, you call
$this->load->library('LoadNews');
$this->loadnews->showList(array('date'=>'2014-12-19'));
Yes, you can do this from CI 3.
In your Controller or View just call $this->load->view(view, data);
Example:
<?php
defined('BASEPATH') or die('No direct script allowed');
class PageController extends CI_Controller
{
public function index()
{
$data = [
'names' => ['john', 'doe'],
'ages' => ['24', '30']
];
$this->load->view('index', $data);
}
}
?>
And in your view you can access 'names' and 'ages' as $names and $ages respectively.
You can do this in your view:
<?php
echo '<pre>';
print_r($names);
print_r($ages);
echo '</pre>';
?>
The result:
Array (
[0] => john
[1] => doe
)
Array (
[0] => 24
[1] => 30
)

Fatal error: Class 'CI_Model' not found on production server, works locally

We're building a web application with CodeIgniter 2.1.4. It's in the crawling stages. Right now, it only has a basic logging and registering system.
What we've so far functions as expected locally, but when we try it online, we get the following error:
Fatal error: Class 'CI_Model' not found in /home4/csurmeli/public_html/other/ems/system/core/Common.php on line 174
It doesn't make any sense since we haven't changed any of the core files. And our online server is well established.
Any suggestions?
The controller calling login:
function login(){
if($this->session->userdata('userid') !== false){
redirect(base_url()."index.php/users/success");
}
$data['error'] = 0;
if($_POST){
$this->load->model('user');
$username = $this->input->post('username',true);
$password = $this->input->post('password',true);
$user = $this->user->login($username,$password);
if(!$user){
$data['error']=1;
redirect(base_url()."index.php/users/error");
}else{
$this->session->set_userdata('userid',$user['userid']);
$this->session->set_userdata('privilege',$user['privilege']);
redirect(base_url()."index.php/users/success");
}
}
$this->load->view('header');
$this->load->view('login',$data);
$this->load->view('footer');
}
Model:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class User Extends CI_Model{
public function __construct(){
parent::__construct();
}
function create_user($data){
if($data['is_sent']){
$query = array("username" => $data['username'],
"password" => $data['password'],
"email" => $data['email']
);
$this->db->insert('users',$query);
}
}
function login($username,$password){
$where = array(
'username'=>$username,
'password'=>$password
);
$this->db->select()->from('users')->where($where);
$query = $this->db->get();
return $query->first_row('array');
}
}
?>
if it works locally - its probably just an issue on the main index.php page
in index.php find the banners: SYSTEM FOLDER NAME and APPLICATION FOLDER NAME
then double check that the file path and the folder names are correct for your server.

How to check all database for availability in CI 2

I have config file database.php with 5 databases.
How can I get 500 error with message "Site is not available" in all pages, if one of a database is not available?
I found it very interesting your question and have been doing some research to solve your problem.
I tell you my solution: the first is to activate the hooks, so in your config.php file make this change:
$config['enable_hooks'] = TRUE;
Once activated the hooks, you need to create a new hook, for it in the file config/hooks.php put something like the following:
$hook['post_controller_constructor'] = array(
'class' => 'DBTest',
'function' => 'index',
'filename' => 'dbtest.php',
'filepath' => 'hooks',
'params' => array(),
);
Thus, your hook kicks in once the controller has been instantiated, but has run no method yet. This is neccesary to use $CI = &get_instance()
To finish create the file /application/hooks/dbtest.php with content similar to the following:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class DBTest {
function index() {
$CI = &get_instance();
$databases = array(
'mysqli://user1:pass1#host1/db1',
'mysqli://user2:pass2#host2/db2',
'mysqli://user3:pass3#host3/db3',
'mysqli://user4:pass4#host4/db4',
'mysqli://user5:pass5#host5/db5',
);
foreach ($databases as $dsn) {
$db_name = substr(strrchr($dsn, '/'), 1);
$CI->load->database($dsn);
$CI->load->dbutil();
if(!$CI->dbutil->database_exists($db_name)) {
// if connection details incorrect show error
show_error("Site is not available: can't connect to database $db_name");
}
}
}
}
You must use dsn for $CI->load->database() in this way we can handle the error instead of Code Igniter when it tries to load the database.
Hope this helps.

Resources