missing login page when redirect - codeigniter

i was create codeigniter controller like this :
function Home() {
parent::Controller();
if(!$this->session->userdata('id'))
redirect ('login');
$this->load->model('Home_Model');
}
but when i try to access the page, the redirect page now show. just show 404 not found but i was create login.php on controller and views also home_model.php on model.
what the problem with that code and how to fix that?
thank you

Because of this line: parent::Controller(), I assume that you are using CodeIgniter 1. Since you are building pages like home and login, I assume that you are starting building a new website. If my assumptions are right, I suggest you using the latest CodeIgniter (v2.1.0 at the moment, required PHP 5.1.6 or newer). You can grab a copy at CodeIgniter Home Page.
First of all, I think you should check your error reporting level. It should be something like error_reporting(E_ALL); for debugging. If not yet, turn it on and run the code again. Maybe you will get your own answer after that :)
After that, you should try the code below for your constructor to check that your home controller process as you expected.
function Home()
{
parent::Controller();
echo 'Home Class Initialized';
}
It should at least show 404 page. If it doesn't print out anything then we need more information. Maybe you should turn on logging and analyze your log files: look for /application/config/config.php: $config['log_threshold'] = 2;
If it print out 'Home Class Initialized' follow by a 404 message: please check if your controller have something like:
public function index()
{
echo 'Invoke default method';
}
If not yet, please add one. You may also want to take a look at using your own default method here
If it still a 404 then sorry, I have no idea. Maybe you should post more code.
If it print out 'Home Class Initialized' only: good. Now try this
function Home()
{
parent::Controller();
if ( ! $this->session->userdata('id'))
{
echo 'Redirecting to login page';
//redirect ('login');
}
$this->load->model('Home_Model');
}
If this print out Redirecting to login page, it's now time for you to uncomment //redirect ('login'); and check the login page. Follow the same path with step 1 & 2.
If not, check your session data: var_dump($this->session->userdata('id'). You could learn more about PHP falsehood here
After all, if it still fails, consider using __construct() of PHP 5
function __construct()
{
parent::__construct();
if ( ! $this->session->userdata('id'))
{
redirect ('login');
}
$this->load->model('Home_Model');
}
Other: if you use this $this->load->model('Home_Model');, I think you will have to call home_model like this $this->Home_Model->foo(); or there will be error access to NULL

Related

Laravel Error 500 on a parametric route when in production

I built a small blog where a editor can create articles and post it and users can read it without login.
The issue that I'm having is the following :
In production server, if a user try to access a blog article from the home by clicking on the 'Read more' button, gets a 500 internal server error.
I'm also using other parametric routes and they are working perfectly fine.
In development server, everything works fine.
Any idea of what could the issue's cause?
Thanks in advance
In the home page I'm rendering a list of all the articles via the PublicController's index() method with the following code :
public function index()
{
$articles = Article::all();
return view('welcome', compact('articles'));
}
And it works perfectly fine.
But when a user click on a item of the list should be redirected, via the 'show()' method of the Public Controller, to the article's details page but in reality the user is getting a 500 internal server error.
The show() method :
public function show($id)
{
$article = Article::find($id);
return view('article', compact('article'));
}
One thing that can help you is to logging the exceptions in your Handler.php file. This way you can see the error details (message, file and code line).

How do i redirect to a page from php yii2.0 components?

I Have a component ,in which have one init() function which is to be called before each action.
I only need to check if the session is expired redirect the user to login.
Here is my code,
config/main.php coponents -
'MyGlobalClass'=>[
'class'=>'common\components\MyGlobalClass'
],
and
'bootstrap' => ['log','MyGlobalClass'],
components/MyGlobalClass .php :
class MyGlobalClass extends \yii\base\Component{
public function init() {
//echo "Hi";
if(Yii::$app->user->isGuest)
{
//echo 'called';exit;
Yii::$app->getResponse()->redirect(Yii::$app->urlManager->createUrl('login'));
}
//parent::init();
}
}
so it is coming inside if{} but not redirecting to login page.
You can try this, it worked for me:
return Yii::$app->getResponse()->redirect('admin/page/signin');
You may use this to send your response
Yii::$app->response->send();
but please review the guide for controller/action distinct access control
http://www.yiiframework.com/doc-2.0/guide-security-authorization.html
You can try the code below:
Yii::$app->getResponse()->redirect(Yii::$app->urlManager->createUrl('login'));
return;
correct way is :
Yii::$app->response->redirect(Yii::$app->urlManager->createAbsoluteUrl('site/logout'));
return;
This way you'll get an infinite redirect loop: The component is called on each controller call, redirection will never stop.
Better to manage it from public function behaviors() on each controller, check the Authorization section on Yii2 documentation.
May be this one help you :)
return Yii::$app->getResponse()->redirect(['user/login']);
or
return Yii::$app->getResponse()->redirect(Yii::$app->urlManager->createUrl('user/login'));

CodeIgniter session not working on Safari

I'm facing a problem in my CodeIgniter application. I'm using set_userdata() to put some information in the session from a specific controller method, and when I try to retrieve this information from another controller method using userdata() it returns empty when using Safari. It works ok when I use Chrome or Firefox.
Here is the controller code:
<?php
class Helloworld extends CI_Controller {
public function display()
{
$this->load->helper('url');
$this->load->library('session');
$this->session->set_userdata('some_name', 'some_value');
//echo $this->session->userdata('some_name');exit; //HERE IT WORKS
redirect("dashboard");
}
public function user_dashboard()
{
$this->load->library('session');
echo "redirect and session test - ";
echo $this->session->userdata('some_name');exit; //HERE IT FAILS
}
}
?>
Here is a snippet code from routes.php:
$route['dashboard'] = 'helloworld/user_dashboard';
So, when I access this URL
http://mydomainhere.com/CI/index.php/helloworld/display
It redirects to this one:
http://mydomainhere.com/CI/index.php/dashboard
The output in Chrome and Firefox is:
"redirect and session test - some_value"
The output in Safari is:
"redirect and session test - "
The strange thing is that it works on Safari if I try to retrieve the information in the session inside the same controller method where I put the information in the session. It doesn't work if I try to retrieve the information from another controller method, in this case I assign the information to the session inside the "display" method and I try to get it from "user_dashboard" method.
I appreciate any help.
Thank you guys.

redirect is not working in codeigniter _construct function

I am facing a problem using redirect in _construct function,
In timesheet controller I wrote the following code and I am getting an error in the browser "
This problem can sometimes be caused by disabling or refusing to
accept
cookies.
Here is my code
class Timesheet extends MY_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('timesheet_model');
//$this->load->library('auth');
$username=$this->session->userdata('logged_in');
//$this->load->model('login_model');
if($username['fullname']!=""){
redirect('timesheet');
}
else{
redirect('login');
}
}
Please help me to find a way to get rid of this problem.
Thanks.
It looks to me like you're going around in a loop.
You check to see if the fullname element in your $username array is empty and, if it is, you redirect back to the same controller. I'm willing to bet it goes around in a circle like that for a while before the webserver throws up the error you mention.
If I'm reading what you're trying to do correctly, wouldn't you call another function within your Timesheet constructor if the fullname element is present to show whatever information it is that you're trying to display?
I'd suggest changing your logic to do the following:
if($username['fullname'] == ""){
redirect('login');
}
else{
//go to another controller method here
}
OK. I got it. The error is just because of I am redirecting to same controller where I have written the code. Everytime when it redirects to timesheet it enters into the construct function and it redirects again to timesheet. And the same thing is working like an endless loop.
So the fault was redirecting to the same controller.
I
use redirect like this : redirect('timesheet', 'refresh');

Why codeIgniter duplicating and appending URL?

When I moved my working CI webapp from my localhost to a webhosting, I encounter a "duplicating and appending URL" problem.
In my localhost, this works (shows the login page): http://mylocal/someapp/ --> which will redirect to http://mylocal/someapp/index.php/login
However, after migrating to webhosting, trying to access it like this: http://webhosting.com/someapp/, somehow it automatically appends to be
http://webhosting.com/someapp/%20//webhosting.com/someapp/index.php/login
My .htaccess contains nothing (works on localhost)
In config.php,
$config['base_url']= '';
The home controller which will redirect to the login controller (then the login view) looks like this:
class Home extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$this->load->view('home_view', $data);
}
else
{
//If no session, redirect to login page
redirect('login', 'refresh');
}
}
function logout()
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('home', 'refresh');
}
}
Or maybe some settings in the webhosting that I need to configure?
Use http:// prefix before base url in $config['base_url'];
e.g.
$config['base_url']=http://localhost/islam;
$config['base_url']= '';
should contain the base url of your website. i think that's the problem.
should be:
$config['base_url']= 'http://yoursite.com/';
Found the answer my self. Turns out that some webhostings are not treating CI refresh properly.
header("Refresh:0;url=...");
instead, it only wants this:
header("Location: ...");
So, I changed url_helper.php (system/helpers/url_helper.php line:543), from
case 'refresh'
to
case 'xxxrefresh'
so that it always skips the header refresh and only use header location.
I don't know if this the proper solution, but it works on my website.
Test your webhosting characteristics before you migrate your CodeIgniter codes from your local machine.
Hope this helps somebody in the future.
I had the same "duplication problem" with a local insallation. Here is my solution: set your base URL in application/config/config.php, for example:
$config['base_url'] = 'http://localhost/ci/';
It is important to add the http:// or https:// prefix to fix the "duplication problem". Of course, the trailing slash is also necessary.

Resources