codeigniter - controller load multiple views - php

I made a website but in two versions one for normal users and one for the mobile users and for both I made view page and also with multilanguage options, first I add in controller
public function index()
{
if ($this->input->get("lang") =="en")
$this->load->view('en_signup');
else
$this->load->view('ar_signup');
$this->load->helper('url');
}
}
I made pages with name of marabic.php and menglish.php for mobile users now first I need to load these pages also but not mix with the original/default view pages, because I already mention java cript in default view page when its detect mobile user it redirect to m.domainname.com now I want to figure out this issue, please suggest.

Try this:
public function index()
{
$this->load->library('user_agent');
$this->load->helper('url');
if ($this->input->get("lang") =="en"){
if ($this->agent->is_mobile()) {
$this->load->view('menglish');
} else {
$this->load->view('en_signup');
}
} else {
if ($this->agent->is_mobile()) {
$this->load->view('marabic');
} else {
$this->load->view('ar_signup');
}
}
}

You can detect if a user is visiting from a mobile device by using CodeIgniter's User Agent library.
$this->load->library('user_agent');
if ($this->agent->is_mobile()) {
// Load mobile view
}

Related

Error 404 despite that page exists Codeigniter

I have something like website.com/profile/nameofuser that is working.
But if I have website.com/profile/_nameofuser I get 404 error also website.com/profile/nameofuser_ or website.com/profile/nameof_user is working. It's not something related to accepted characters, but what's the problem?
class Profile extends CI_Controller {
public function __construct()
{
parent:: __construct();
$this->load->model('Profile_model');
$this->load->helper(array('url', 'form', 'htmlpurifier'));
}
public function index() {
$this->load->library('form_validation');
if(getUserData($this->uri->segment(2), "ID") < 0) {
$this->session->set_flashdata('error', 'Profil inexistent.');
redirect(base_url());
}
if (!is_cache_valid(md5('profile' . $this->uri->segment(2) . ''), 300)){
$this->db->cache_delete('profile', $this->uri->segment(2));
}
if(getUserData($this->uri->segment(2), "ID") > 0) {
/* some mysql queries.. */
}
$data["main_content"] = 'profile/profile_view';
$this->load->view('includes/template.php', $data);
} else {
$this->session->set_flashdata('error', 'Profil inexistent.');
redirect(base_url());
}
}
function _remap($method,$args)
{
if (method_exists($this, $method))
{
$this->$method($args);
}
else
{
$this->index($method,$args);
}
}
}
Here is my profile controller. I really don't know what's the problem. If I enter an invalid profile redirects with error flashdata so ti's ok. Maybe it's a remap problem?
1) Check the fie format in your view file.. if it is html file that mean you cannot call it without its format
for example
if your file is php format have name home-view.php
you can call it as
$this->load->view('home-view');
but if it is html file then the name in home-view.html
so you have to call it with its exstention as
$this->load->view('home-view.html');
your call to /profile/nameofuser is missing a fundamental from the MVC architecture.
You need to call a controller/method combination (in CI, your basic url is domain.com/controller/method)...
since you don't have a specific method for each user within the profile controller, which is actually a good thing, you need a method within the controller that handles your users. You already have it, it's called index
If you point your URL to /profile/index/nameofuser and change $this->uri->segment(2) to $this->uri->segment(3) you should get it to work
give it a try and let me know

Server Check Codeigniter

My redirect and server check not working for my index.php in codeigniter.
I have a front end install wizard for codeigniter with 4 steps the 4th step being the last. I am trying to make it so if user has gone through the steps and has reached step 4 that will have access to website on there domain/server.
Currently the code I use keeps on redirecting to the index.php of my install URL, even though I have completed all steps.
This code below is on my main index.php
if (!file_exists('install/index.php')) {
// Make sure we've not already tried this
if (strpos($_SERVER['REQUEST_URI'], '/install/')) {
header('Status: 404');
exit;
}
// Otherwise go to installer
header('Location: '.rtrim($_SERVER['REQUEST_URI'], '/').'/install/');
exit;
}
Welcome Controller
public function index()
{
if (!file_exists('install/index.php')) {
// Make sure we've not already tried this
if (strpos($_SERVER['REQUEST_URI'], '/install/')) {
header('Status: 404');
exit;
}
// Otherwise go to installer
header('Location: '.rtrim($_SERVER['REQUEST_URI'], '/').'/install/');
exit;
} else {
$this->load->view('welcome_message');
}
}
You can place this code in the welcome controller
Place your code inside the index function of the controller
class Welcome extends CI_Controller {
public function index()
{
/* your redirect code*/
}
public function comments()
{
echo 'Look at this!';
}
}

Logout ,Session Expiration

Hi I am developing a website using codeigniter,php that requires secure login, The problem arises when I logout, & firstly I destroy the session but trouble is, when I click back on the browser it is displaying the Login page again.. Thanks for answers in advance
Following is my index function &
website is my controller .
public function index()
{
if(logged_in() )
{
redirect('/website/dashboard');
}
else
{
redirect('/website/login');
}
}
code works for me.. But when i logout from site & press back button i am seeing my dashboard again...
my log out function :
public function logout($redirect = false)
{
$this->CI->session->sess_destroy();
if($redirect)
{
$this->CI->load->helper('url');
redirect($redirect, 'refresh');
}
}
I suggest you to add something like following code to each of your VIEW page
<?php
if($this->session->userdata('session_set')!='true' )
{
redirect('/website/login');
}?>

How do I set a return URL during login on Codeigniter / Tank_Auth?

The issue here is that when my user logs into my app, they always are redirected to the default controller.
I would like the user to be redirected to the page they were on before logging in.
So for example, if the user is reading forum post #12 (reading does not require login) and then decides to post an answer (answering requires login), once they login they should go back to post #12.
I am using PHP/Codeigniter 2.0.2 and the Tank_Auth library, and have in several of my controllers
function __construct()
{
parent::__construct();
if (!$this->tank_auth->is_logged_in()) {
redirect('/auth/login/');
} else {
//load stuff
}
My question is
What is the best way to set a return URL (Cookie? GET?), and how would that be implemented?
If you're familiar with Tank_Auth, in which files should I make these changes?
Any roadmaps are welcome, even if you don't use Tank_Auth.
I Recently implemented this solution on a webpage I was working.
In the controller/auth file add a reference to the user_agent library:
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->library('security');
$this->load->library('tank_auth');
$this->lang->load('tank_auth');
$this->load->library('user_agent'); //This is the line you are adding
}
In the views/auth/login_form.php and taking advantage of the CodeIgniter's user_agent library add a hidden input tag which will contain the referrer url as follows:
<?=form_hidden('redirect_url', $this->agent->referrer());?>
<?php echo form_submit('submit', 'Let me in'); ?>
<?php echo form_close(); ?>
After that, all you have to do is redirect the users to the content of the input named "redirect_url" when the user posts the login data to the login action:
/**
* Login user on the site
*
* #return void
*/
function login()
{
/*.... Beginning of the login action function...
....
....
*/
if ($this->tank_auth->login(
$this->form_validation->set_value('login'),
$this->form_validation->set_value('password'),
$this->form_validation->set_value('remember'),
$data['login_by_username'],$data['login_by_email'])) //valid
{
redirect( $this->input->post('redirect_url'));
}
}
This works great for me... It's fine and simple. I believe it can help you.
Let me know about anything.
This is the solution I've been using with tank_auth, it's probably not the best, but I've found it works well for me.
In the controller
if (!$this->tank_auth->is_logged_in()){
$encoded_uri = preg_replace('"/"', '_', $_SERVER['REQUEST_URI']);
redirect('/login/'.$encoded_uri);
}elseif($this->tank_auth->is_logged_in(FALSE)){ // logged in, not activated
redirect('/user/reactivate/');
}else{
//Logged IN Stuff Here
}
Modified Tank Auth Login Function (controllers/auth.php)
function login($return_to = "")
{
if ($this->form_validation->run()) {
if ($this->tank_auth->login(
$this->form_validation->set_value('login'),
$this->form_validation->set_value('password'),
$this->form_validation->set_value('remember'),
$data['login_by_username'],
$data['login_by_email'])) {
//...Other Stuff Here
$decoded_uri = preg_replace('"_"','/',$return_to);
redirect($decoded_uri);
}
}
}
You may need to change the preg_replace to something else if your urls have _ in them, I just used that because it works for me
EDIT
I've updated the function, this is one from another project that we heavily modified the tank auth stuff, so if stuff is a bit different, I'm sorry
As for the passing the encode_uri stuff, I've added the following to the routes.php file (config/routes.php)
$route['auth/login/(:any)'] = 'auth/login/$1';
$route['auth/login'] = 'auth/login'; //Probably don't need this one now
Hi I solved it as follows
In your controller
Add this: $this->load->library(array('tank_auth');
if (!$this->tank_auth->is_logged_in()) {
$encoded_uri = preg_replace('"/"', '_', $this->uri->uri_string());
redirect('/auth/login/'.$encoded_uri);
} else {
// Logged IN Stuff Here
}
In Tank Auth Controller (controllers/auth.php)
function login($return_to = "")
{
if ($this->form_validation->run()) {
if ($this->tank_auth->login(
$this->form_validation->set_value('login'),
$this->form_validation->set_value('password'),
$this->form_validation->set_value('remember'),
$data['login_by_username'],
$data['login_by_email'])) {
// success
$decoded_uri = preg_replace('"_"','/',$return_to);
redirect($decoded_uri);
}
}
}
I replaced $_SERVER['REQUEST_URI'] with this $this->uri->uri_string() because that allow you get /controller/method/...etc. to redirect later in tank auth controller
That work perfect for me and how said #Cubed Eye "You may need to change the preg_replace to something else if your urls have _ in them"
Thanks to #Cubed Eye
I hope this helps someone else too.

SEO url gives 404-error in CodeIgniter

I am pretty new to codeigniter. I do know php.
How can I accomplish to load the right view?
My url: /blog/this-is-my-title
I’ve told the controller something like
if end($this->uri->segment_array()) does exist in DB then load this data into some view.
I am getting an 404-error everytime I access /blog/whatever
What am i seeing wrong?
unless you're using routing, the url /blog/this-is-my-title will always 404 because CI is looking for a method called this-is-my-title, which of course doesn't exist.
A quick fix is to put your post display code in to another function and edit the URLs to access posts from say: /blog/view/the-post-title
A route like:
$route['blog/(:any)'] = "blog/view/$1";
may also achieve what you want, if you want the URI to stay as just `/blog/this-is-my-title'
The may be more possibilities:
The most common - mod_rewrite is not active
.htaccess is not configured correctly (if u didn't edited it try /blog/index.php/whatever)
The controller does not exist or is placed in the wrong folder
Suggestion: if you only need to change data use another view in the same controller
if (something)
{
$this->load->view('whatever');
}
else
{
$this->load->view('somethingelse');
}
If none of those works post a sample of code and configuration of .htaccess and I'll take a look.
The best way to solve this problem is to remap the controller. That way, you can still use the same controller to do other things too.
No routing required!
enter code here
<?php
class Blog extends Controller {
function __construct()
{
parent::__construct();
}
public function _remap($method, $params = array())
{
if (method_exists($this, $method))
{
$this->$method();
}
else
{
$this->show_post();
}
}
function index()
{
// show blog front page
echo 'blog';
}
function edit()
{
// edit blog entry
}
function category()
{
// list entries for this category
}
function show_post()
{
$url_title = $this->uri->segment(2);
// get the post by the url_title
if(NO RESULTS)
{
show_404();
}
else
{
// show post
}
}
}
?>

Categories