I'm having a hard time debugging to locate the issue here.
I've tried echoing $login_by_username and $login_by_email but they aren't echoing.
The problem is when it loads the login form it always puts email as the label.
Controller:
$login_by_username = $this->config->item('login_by_username', 'config1');
$login_by_email = $this->config->item('login_by_email', 'config1');
$this->data['login_by_username'] = $login_by_username;
$this->data['login_by_email'] = $login_by_email;
View:
<?php
if ($login_by_username AND $login_by_email)
{
$login_label = 'Email or Username';
}
else if ($login_by_username)
{
$login_label = 'Username';
}
else
{
$login_label = 'Email';
}
?>
<?php echo form_label($login_label, 'login'); ?>
Config:
$config['login_by_username'] = TRUE;
$config['login_by_email'] = TRUE;
According to our comments.
You have used the autoload.php to call your config file.
You made some tests to make sure that the problem is with your config1.php file and not with CI.
I've made a test and here is what I did.
I have created a file config1.php inside: application/config.
Here it is:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['teste'] = 'config1 test';
On my autoload.php also inside application/config:
$autoload['config'] = array('config1');
My Home Controller:
public function index()
{
$data = array('config'=>$this->config->item('teste'));
$this->load->view('welcome_message', $data);
}
My view:
<?php echo $config;?>
And config1 test is echoed as expected.
Make sure that config items login_by_username and login_by_email are there in the config1.php file
Make sure that you are passing $this->data while loading the view eg:
$this->load->view('view.php', $this->data)
Related
I'm working on user login system in codeigniter. When user clicks on login button the username and password are verified and the data is store in $this->session->set_userdata($arr); session library and the page redirects to home.php. Till here works fine but when is try to access the session in home.php as print_r($this->session->userdata);. it give me this errors
Here is the Controller code : Login.php
function login_user(){
$login_btn = $this->input->post('login_btn');
if($login_btn == TRUE){
$arr['login'] = array(
'username'=>$this->input->post('username'),
'password'=>$this->input->post('password')
);
$query = $this->Login_model->users_login($arr);
if($query->num_rows() > 0 && $query->num_rows() == 1){
$this->load->library('session');
$this->session->set_userdata($arr);
redirect('Login/redirec_To_home','refresh');
}
else
{
$this->index();
}
}
else
{
$this->index();
}
}
function redirec_To_home(){
$this->load->view('login/home');
}
Here is the view html file name : path views/login/home.php
<?php
print_r($this->session->userdata); //ERRORS SHOWN IN THIS LINE NUMBER : 13
?>
THANKS IN ADVANCE
Please go to application/config/autoload.php in your project directory and search libraries, after that add session to this line which will look like this.
$autoload['libraries'] = array('session');
In the __construct area you need to reload the session library using get instance in libraries
https://www.codeigniter.com/user_guide/general/ancillary_classes.html
https://www.codeigniter.com/user_guide/general/creating_libraries.html#utilizing-codeigniter-resources-within-your-library
class Example {
protected $CI;
// We'll use a constructor, as you can't directly call a function
// from a property definition.
public function __construct()
{
// Assign the CodeIgniter super-object
$this->CI =& get_instance();
$this->CI->load->library('session');
$this->CI->load->model('login_model');
}
public function somefunction()
{
$this->CI->input->post('username');
$this->CI->input->post('password');
$user_model = $this->CI->login_model->user_login();
$sesstion_data = array('user_id' => '1');
$this->CI->session->set_userdata($sesstion_data);
}
}
I'm trying to get my head around custom URIs. I would like my view_county to have the URL:
http://localhost/chipadvisor2/controller_ca/all_county/1
and my view_all to have the URL:
http://localhost/chipadvisor2/controller_ca/all_chippers
If anyone can help me I would greatly appreciate it. The link works for view_county but when I click on the view_all link it shows:
{
"status": false,
"error": "Unknown method"
}
and if I click into view_county first then the view_all link it just keeps adding controller_ca/all_chippers to the URL and doing nothing. Any ideas why?
model_ca
<?php
class Model_ca extends CI_Model {
function __construct() {
parent::__construct();
}
function get_chipper() {
$query = $this->db->get('chipper_reviews');
return $query->result();
}
function get_county($id) {
$this->db->where('location', 'Westmeath');
$query = $this->db->get('chipper_reviews');
return $query->result();
}
}
?>
controller_ca
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH.'/libraries/REST_Controller.php';
class Controller_ca extends REST_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->model('model_ca');
}
public function index_get() {
$this->all_chipper_get();
}
public function all_chipper_get() {
$data['chipper'] = $this->model_ca->get_chipper();
$this->load->view('view_all', $data);
}
public function all_county_get() {
$id = $this->uri->rsegment(3);
$data['location'] = $this->model_ca->get_county('location',$id);
$this->load->view('view_county', $data);
}
}
?>
view_county
and
view_all
<html>
<head> </head>
<body>
<div class="container">
View All Chippers
<br/>
View County
<?php echo "<br/><br/>";
foreach ($chipper as $chipperdata) {
echo "<b>Name: </b>".$chipperdata->name." "."<br/>"."<b>County: </b>".$chipperdata->location." "."<br/>"."<b>Description: </b>".$chipperdata->description." "."<br/>"."<br/>";
} ?>
</div>
</body>
</html>
I have added this to the route.php file also but don't know how to put it into effect or how to add the value 'westmeath' to the end. Ideally I would like to pass it as a variable rather than hardcoding it:
$route['default_controller'] = 'Controller_ca';
$route['404_override'] = '';
$route['translate_uri_dashes'] = false;
$route['all_chipper'] = 'controller_ca/all_chippers';
$route['all_county'] = 'controller_ca/all_county/$1';
Try to give controller method name same as url. Change all_chipper_get() to all_chipper(). Same for other method and check.
Regarding the issue:
and if I click into view_county first then the view_all link it just
keeps adding controller_ca/all_chippers to the URL and doing nothing.
Any ideas why?
Please add base_url() at the starting of URL in anchor tag. It will resolve the append issue in URL. Example:
View All Chippers
<br/>
View County
Also, change your routing as:
$route['controller_ca/all_county'] = 'controller_ca/all_county/$1';
$route['controller_ca/all_chipper'] = 'controller_ca/all_chippers';
I'm trying to access my file where it contains a home page using a function from another file (redirect.php). MyWebsite/main/main.html this is the location of my file that I want to access. I stuck in here redirect::toMain('main.html'); don't have a idea how to link the full path so I will be directing to the home page. I tried to pass ('main.html') value to the function toMain() but it didn't work. Location of folder below.
So MyWebsite is the main folder and it contains sub-folder listed below.
function (folder) contains a redirect function
main (folder) contains a html. main.html where it set as my home page. This is where the user will redirect.
Class: redirect.php Location: MyWebsite/function/redirect.php
<?php
class redirect
{
public static function toMain($location = null)
{
if($location)
{
header('Location: ' . $location);
exit();
}
}
}
$a = new redirect();
?>
loginProcess.php
<?php
session_start();
require_once('../connection/connection.php');
require_once('../connection/loginCRUD.php');
require_once('../function/redirect.php');
if(isset($_POST['submit']))
{
$dbusername = $_POST['loginUsername']; //Store value of textfield in array
$dbpassword = $_POST['loginPassword'];
if(!empty($dbusername) && !empty($dbpassword))
{
if((new loginCRUD)->readLogin($dbusername,$dbpassword)) //If true
{
// echo "You are logged in!";
$_SESSION['loginUsername'] = $dbusername;//Session are stored in array.
// redirect the user to the home page
redirect::toMain('main.html');
}
else
{
echo "Incorrect username and/or password";
}
}
}//end of isset
?>
Here is one way that I would do this:
class redirect
{
private static $baseUrl = 'http://localhost/MyWebsite/main/';
public static function toMain($location = null)
{
if($location)
{
header('Location: ' . self::$baseUrl . $location);
exit();
}
}
}
The $baseUrl can also be this:
private static $baseUrl = '/MyWebsite/main/';
Make sure that MyWebsite starts with http:// or it will redirect to a relative URL. You don't need $a = new redirect(); since you are accessing the methods statically.
Now you can do this:
redirect::toMain('main.html');
I am working on a basic application using the CI framework.
I have the following error:
404 Page Not Found
The page you requested was not found.
Posted below are my code files.
My Controller code:
class Contact extends CI_Controller{
function _Contact(){
parent::CI_Controller();
}
/*function main(){
$this->load->model('contact_model');
$data = $this->books_model->general();
$this->load->view('books_main',$data);
}*/
function input(){
$this->load->helper('form');
$this->load->helper('html');
$this->load->model('contact_model');
if($this->input->post('mysubmit')==true){
$this->contact_model->entry_insert();
}
$data = $this->contact_model->general();
$this->load->view('contact_input',$data);
}
}
Then in Model I have the following code:
class contact_model extends CI_Model{
function _contact_model(){
parent::Model();
$this->load->helper('url');
}
function entry_insert(){
$this->load->database();
$data = array(
'name'=>$this->input->post('title'),
'address'=>$this->input->post('author'),
'year'=>$this->input->post('year'),
);
$this->db->insert('contact',$data);
}
function general(){
$data['base'] = $this->config->item('base_url');
$data['name'] = 'Name';
$data['address'] = 'Address';
$data['year'] = 'Year';
$data['years'] = array('2007'=>'2007',
'2008'=>'2008',
'2009'=>'2009');
$data['forminput'] = 'Student Registration';
$data['fname'] = array('name'=>'name',
'size'=>30
);
$data['faddress'] = array('name'=>'address',
'size'=>30
);
return $data;
}
}
Finally, my View:
<html>
<head>
</head>
<body>
<div id="header">
<?php $this->load->view('contact_header'); ?>
</div>
<?php echo heading($forminput,3) ?>
<?php echo form_open('books/input'); ?>
<?php echo $name .' : '.
form_input($fname).br(); ?>
<?php echo $address .' : '.
form_input($faddress).br(); ?>
<?php echo $year .' : '.
form_dropdown('year',$years).br(); ?>
<?php echo form_submit('mysubmit','Submit!'); ?>
<?php echo form_close(); ?>
<div id="footer">
<?php $this->load->view('contact_footer'); ?>
</div>
</body>
</html>
Can any one please help me?
Remove this in your Contact Controller:
function _Contact(){
parent::CI_Controller();
}
Replace with this:
function __construct(){
parent::__construct();
}
And in your Contact Model remove this:
function _contact_model(){
parent::Model();
$this->load->helper('url');
}
Replace it with this:
function __construct(){
parent::__construct();
$this->load->helper('url');
}
Hey,
I often had similar Problems. Usually I check the following:
Check the Config file if the correct siteroot is set. I often had live server stuff in there.
Check in the .htaccess if the RewriteBase is set to the correct directory.
to make sure its reading the correct value an echo base_url(); (if this works its usually the .htaccess rewrite base).
Hope it helps.
r n8m
[enter link description here][1]
[1]: http://ellislab.com/forums/viewthread/105880/
hmc
If you have developed your application on Windows, you might have not set first character of Controllers and Models name as a capital character.
like /controllers/home.php to /controllers/Home.php
On Linux file names are case sensitive.
Note:- This is a solution to a possible problem. There may be issues of mis-configuration, server and path variables.
Possible Duplicate: CodeIgniter "The page you requested was not found." error?
I am using Kohana 3.2 and I am having problems calling the ouput of a controller in another controller.
What I want...
In some pages I have got a menu, and in others I don't. I want to use make use of the flexability of the HMVC request system. In the controller of a page I want to call another controller which is responsible for the creation of the menu.
What I have a the moment:
file menu.php:
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Menu extends Controller
{
private $_model = null;
public function __construct(Request $request, Response $response)
{
parent::__construct($request, $response);
$this->_model = Model::factory('menu');
}
public function action_getMenu()
{
$content = array();
$content['menuItems'] = $this->_model->getMenuItems();
// Render and output.
$this->request->response = View::factory('blocks/menu', $content);
//echo '<pre>'; print_r($this->request->response->render()); echo '</pre>'; die();
}
}
somepage.php
public function action_index()
{
$this->template->title = 'someTitle';;
$contentData['pageTitle'] = 'someTitle';
$contentData['contentData'] = 'someData';
#include the menu
$menuBlock = Request::factory('menu/getMenu')->execute();
$menuData = array('menu' => $menuBlock);
$this->template->menu = View::factory('pages/menu')->set('menu',$menuData);
$this->template->content = View::factory('pages/somePage', $contentData);
$view = $this->response->body($this->template);
$this->response->body($view);
}
If I uncomment the following line in menu.php, I see the menu rendered:
//echo '<pre>'; print_r($this->request->response->render()); echo '</pre>'; die();
So I guess that part is alright. The problem is in the following line in somepage.php:
$menuBlock = Request::factory('menu/getMenu')->execute();
This gives me back a response object. Whatever I do, I do not get the output in $this->template->menu.
$this->template->menu = View::factory('pages/menu')->set('menu',$menuData);
What must I do to have $this->template->menu contain the view, so I can use it correctly?
I hope this all makes sense. This is the way I would like to do it, but maybe I am completely on the wrong track.
I would do it this way:
class Controller_Menu extends Controller
{
public function action_build()
{
// Load the menu view.
$view = View::factory('navigation/menu');
// Return view as response-
$this->response->body($view->render());
}
}
In your controller get the menu as follows:
// Make request and get response body.
$menu = Request::factory('menu/build')->execute()->body();
// e.g. assign menu to template sidebar.
$this->template->sidebar = Request:.factory('menu/build')->execute()->body();
I would not use the __construct method in your controllers. Use before() instead, this is sufficient for most of the problems (for example auth):
public function before()
{
// Call aprent before, must be done here.
parent::before();
// e.g. heck whether user is logged in.
if ( !Auth::instance()->logged_in() )
{
//Redirect if not logged in or something like this.
}
}
I found the answer to my problem in less than an hour after asking.
I just forgot to put it here.
In somePage.php change :
$menuBlock = Request::factory('menu/getMenu')->execute();
$menuData = array('menu' => $menuBlock);
$this->template->menu = View::factory('pages/menu')->set('menu',$menuData);
To:
$this->template->menu = Request::factory('menu/getMenuBlock')->execute()->body();
And in menu.php change:
$this->request->response = View::factory('blocks/menu', $content);
To:
$request = View::factory('blocks/menu', $content);
$this->response->body($request);
I hope this will help someone else.