Including directory and keep it working php - php

I'm using Codeigniter and facing the following problem.
In a controller, I want to include in some way an application (Not written in Codeigniter) in my controller. I am using file_get_contents now. It's working fine and the application is shown in the controller I made. The problem is, the application contains a lot of forms which are posted to other pages in the application itself. Using file_get_contents, the forms redirect the user to a target outside my controller. This may sound a bit vague, so here's an example:
The user navigates to the controller: 'game/login' and therefore wants
to visit the login page. The user fills in the form. The form posts
the data to assets/game/index.php?page=login. This makes the user
redirect to assets/game/index.php?page=login, and therefore making him
leave the controller page.
Does anyone have an idea on how to fix this problem?
defined('BASEPATH') OR exit('No direct script access allowed');
class Game extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function index($page = ''){
file_get_contents(FCPATH . 'assets/game/index.php?page=' . $page);
}
}

You can load your external php pages in a iframe but i would suggest you should convert your php files to codeigniter MVC structure to get the advantages.
The quick way is using iframe,but i would ask you to refer this post to get an idea on Iframes when to use & when not to.
Are iframes considered 'bad practice'?

Related

How to separate frontend and admin in CodeIgniter framework?

I started working on a project which should have admin panel and frontend and I want to use CodeIgniter framework on client request. But the problem is I am not able to understand how to start the project as mentioned above.
I want folder similar to the image shared
Besides the Admin_controller (for separeted security rules), for better organization, it's good to use some extension like this one:
HMVC: https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/src
With this you'll be able to that this type os structure:
URLs
http://awesome.site/public_controller
http://awesome.site/*module_name*/*controller_inside_module*
http://awesome.site/admin/login
Try using Codeigniter's session functionality to authenticate the user and his role (e.g., "admin", "customer", etc)
Then add a constructor like this to every controller (this is just an example)
class Admin_only extends CI_Controller {
public function __construct()
{
parent::__construct();
if( !isset($this->session->userdata['logged_in']) || $this->session->userdata['logged_in']['user_type'] != 'administrator' )
{
// you're not welcome here
redirect('welcome/access_error');
}
}
The __construct() is run every time anything within the controller is accessed.
See how in my example (there's cleaner ways, but this will definitely work), I'm constantly checking if the user is logged in AND if the user is an administrator (actually I'm checking the opposite... logged out OR not administrator, but it's pretty much the same thing logically) and if the check fails, the user is redirected away from the controller.

Codeigniter Session is not working when uploaded to host

I have created a PHP project using Codeigniter and its working perfectly with the localhost. I'm using Xampp 3.2.1 and when I upload the project to the server and try to load the project its working and show the login page. When I enter credentials and login, it redirect me to the home page of my project and when I try to navigate to any other location it redirects me to login page. Please can any one help me on this matter?
This cause because Codeigniter In-built session is not supported in the sever. What you can do is Use PHP Sessions
refer to this link if you want more details -
http://www.php.net/manual/en/book.session.php
What you can do is use start_session() in you controller and use $_SESSION to save your session data and access it.
If you have More than one Controller the approach is different,
You have to create a controller in your project's \application\core\ call MY_Controller(Its okay if you want to use a different Name). The Code of the Controller Should be
<?php
class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
if(!isset($_SESSION))// to avoid A session had already been started - ignoring session_start()
{
session_start();
}
}
}
?>
and extend all your controllers in your \application\controllers\ with this controller in order to access the session globally
Now use
$_SESSION['data_name'] = $Your_Data;
to set values to session
and in log out function just use session_unset() to clear your current session data
Hope It helps :)

CodeIgniter building a CMS with different page types

I've just started to get into MVC with PHP and have had a good mess about with the likes of CodeIgniter and CakePHP. I'm interested to find out what people's approaches to the following would be:
Normally when I have built a website with a CMS in the past I have approached it by having a core URI table in my database. Each URI is unique and represents a page on my website (e.g. www.example.com/about would reference a record in my URI table with 'about' as the unique URI). The URI table also contains a 'type' column which tells the system what type of page it is (e.g. splash, basic, gallery or whatever). Each 'type' has a corresponding table in my database with all the data for records of that type in them (e.g. I would have tables: basic, gallery and splash). The type also tells the system which template/pagehandler to load which in turn does what it needs to do for each page type.
So if you go to www.example.com/about, my system looks in my URI table for a record with URI 'about', finds it's type to be 'basic' so it loads the basic template/pagehandler which uses the basic table in my database to load and render the page. In the CMS I follow a similar approach, I'll have add/edit forms for all of the different types in my page manager.
I was wondering how you would approach this using an MVC framework such as CodeIgniter? I essentially want to have different controllers for each type of page on both the front and back. However, when someone get's to my site, they will end up on a URI with a single level so I need to check the type of the page and pass off to the correct controller. Is there a way you would recommend of checking the type of each page and then loading the relevant controller to do the rest of the work?
My approach eventually was to extend the _parse_routes() method of the Router class to check the database for any records matching the current uri and set the request with the corresponding value from the database.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Router extends CI_Router {
function __construct() {
parent::__construct();
}
function _parse_routes() {
require_once( BASEPATH .'database/DB'. EXT );
$db =& DB();
$routes_table_exists = $db->query("SHOW TABLES LIKE 'routes';");
if ($routes_table_exists->num_rows > 0) {
$uri_routes = $db->get_where('routes', array('uri' => $this->uri->uri_string()));
if ($uri_routes->num_rows > 0) {
$row = $uri_routes->result()[0];
if (isset($row->request)) {
return $this->_set_request(explode('/', $row->request));
}
}
}
parent::_parse_routes();
}
}
Whether or not it's the best approach to take it seems to work so far.
Usually its a combination of Routes and naming your controllers. so for example you have an About page, and you don't need a separate About controller. Lets say you have a general Pages controller, and then a view($page) method to retrieve and show the page.
example.com/about
$route['about'] = "pages/view/about";
if you just have a few pages there are advantages to hard coding the routes - it protects your database. but otherwise taking an example from the tutorial
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
this does the same thing but now it will take example.com/anything can go here
Versus something like a contact page - where you probably want to have a separate controller called Contact, because you will need to validate the contact form, add it to a database, email it, show a response, show the form again if did not validate, etc So then you can just do a simple link to show the contact form: example.com/contact
the contact form submits to: example.com/contact/submit
more about Routes
http://ellislab.com/codeigniter/user-guide/general/routing.html
and definitely look at the tutorial it will give you more examples about routes
http://ellislab.com/codeigniter/user-guide/tutorial/index.html

Phil Sturgeon’s Template Library with Theme Issue

Even though PyroCMS is an excellent CMS to use and I highly recommend anybody and everybody using when the situation grants it.
However, my situation calls for a custom CMS that just has quite a bit of differences that I am instead developing mine with the use of some of the libraries I see out there that are free to use. The main one I am currently working with right now is Phil Sturgeon’s Template library.
Now since obviously he uses it for PyroCMS I am trying to match up as close to a file structure as he does with that so that the template system will flow smoothly without any issues.
I am currently in a quandary right now because I have my login form is not finding a partial and I’m not quite sure why. When my dashboard calls the metadata partial it loads it up just fine but something is amidst and is not loading it when I call it in the login form.
Here is my current login controller, login form view, and the file structure to ensure it is set up properly.
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends Backend_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->template
->set_layout(FALSE)
->build('login');
}
}
<head>
<!-- metadata needs to load before some stuff -->
<?php file_partial('metadata'); ?>
</head>
applications/
themes/
supr(my custom theme from template)/
views/
login.php
partials/
metadata.php
Any ideas from anyone?
In your controller:
$this->template
->set_layout(FALSE)
->set_partial('metadata', 'partials/metadata.php')
->build('login');
}
note the added call to set_partial.
In your view:
<?php echo $template['partials']['metadata']; ?>

CodeIgniter "default_controller" functions without controller name in URL

Forgive me if this is a "duh" question or if you need more information to answer, I am new to CodeIgniter and still haven't figured out a few things with best practices and such...
In routes.php I have $route['default_controller'] = "home"; so my default_controller is obviously "home".
Inside my home.php controller I have:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
function __construct() {
// blah
}
function index(){
// blah
}
function login(){
// blah
}
}
Which works fine and everything, there is no problems with that. The only thing I can't figure out is if I want to access the login function, I currently have to go to www.blah.com/home/login. How can I change it so it just goes to www.blah.com/login, without creating a new controller (I would like to keep some of these one time base urls all in my default controller)? Is that even possible or do I just have to create a new controller?
If I just have to create a new one, is there a best practices for how many controllers you have, etc.
Documentation says: This route will tell the Router what URI segments to use if those provided in the URL cannot be matched to a valid route.
So use $route['login'] = 'home/login';
The way I have it set up is to have the index function act as the gatekeeper: if not logged in, show a login form view, if logged in, redirect to your (protected) home page. The login function is only accessed via post, when the user submits his login/pwd, and performs the login logic.
You need add a line to your application/config/routes.php file
$route['login'] = "home/login";

Categories