Quantcast
Channel: CodeIgniter Forums - All Forums
Viewing all 14058 articles
Browse latest View live

$config['csrf_protection'] = FALSE; if true my website error

$
0
0
Hello,
I already have a website running well, but in order to make it securer , I do this :

In config php, it comes   $config['csrf_protection'] = FALSE;
I change to = TRUE , but that error happen 

DataTables warning: table id=mytable - Ajax error. For more information about this error, please see http://datatables.net/tn/7

Is there something i must do when changing from false to true?

Routes with regex and namespace

$
0
0
Hi,

I am having trouble defining my routes for my admin pages.

I have multiple Controllers in my app/Admin folder with Namespace App\Admin (including Dashboard and Order)

Here is an example Order.php :

PHP Code:
<?php
namespace App\Admin;
use 
CodeIgniter\Controller;
 
class 
Order extends Controller{
   function 
index(){
       echo 
view('admin/order_view');
   }



What I wanted to achieve was :

PHP Code:
$routes->get('/''Home::index');
$routes->group('admin', ['namespace' => 'App\Admin'], function($routes)
{
 
   $routes->get('/''Dashboard');
 
   $routes->get('([a-zA-Z_]+)''$1');
}); 

to get the default index function of my \App\Admin\Order controller.

I did notice the $0, $1 ... syntax only works when there is at least one forward slash in front of my route "/([a-zA-Z_]+)" which I think is unfortunate.

I can easily show the Dashboard (example.com/admin) that is hardcoded but can't seem to get the Order controller (example.com/admin/Order or even example.com/admin/order) through the regex, although they are in the namespace and folder.

It seems that you can use a lower case first letter when you want to get a controller through its filename, but when it comes to namespaces it's case sensitive or am I wrong?

I also tried

PHP Code:
$routes->get('/''Home::index');
$routes->group('admin', ['namespace' => 'App\Admin'], function($routes)
{
 
   $routes->get('/''Dashboard');
});

$routes->get('admin/([a-zA-Z_]+)''\App\Admin\$1'); 


but alas it doesn't work either. I was hoping to use the group feature so that I can easily add other routes through regex like :
PHP Code:
$routes->get('([a-zA-Z_]+)/edit' '$1::edit' ); 

or

PHP Code:
$routes->get('([a-zA-Z_]+)/add' '$1::add' 

which doesn't seem too farfetched.

Custom ORM - Suggestions

$
0
0
Hello,

I switched from Laravel to CodeIgniter, I really like simplicity" and freedom that CI brings.

Since I only miss Eloquent in CodeIgniter I started working on my own. My plan isn't something advanced, I want to have relationships and some methods like isSomething().

How it works:

In models I have methods like all(), find($id)... In those methods are $this->db queries. Also, I have Entities. You can see that I run foreach in all() method and there I create Entity instances.

I really like this because I can have relationships or some methods that I can access everytime without writing new queries. For example, I have method isPublic() that checks if column "access_level" isn't set to "private".

There should be something like BaseEntity class or interface that each Entity will extend/use but this is just a prototype. The one thing that really annoys me is this "require_once" call at the top. Is there a better way to load those classes?

I would like to know what do you think about this and what should be improved


Thanks. I appreciate any suggestions.

(sorry if I posted in a wrong subforum)

Controller:

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Welcome extends CI_Controller {
    public function 
index()
    {
        
$data = ["content"=>"welcome_message","users"=>$this->user_model->all()]; // Users will be array of Entity instances
        

        // Serve different index if an user is logged in //
        
if($this->session->user) {
            
$this->load->model("link_model");
            
            
$data["user"] = $this->user_model->find($this->session->user->id); // Entity instance
            
$data["content"]="user/index";

        }
        
//
        
$this->load->view('templates/layout',$data);
    } 


Model:

PHP Code:
<?php

require_once(APPPATH.'entities\User_entity.php');

class 
User_model extends CI_Model {

 
   public function all() {
 
       $query $this->db->get("users");
 
       $results = [];
 
       foreach($query->result() as $row) {
 
           $results[] = new User_entity($row);
 
       }
 
       return $results;
 
   }

 
   public function find($id) {
 
       $query $this->db->get_where("users",$id);
 
       foreach($query->result() as $row) {
 
           return new User_entity($row);
 
       }
 
   

Entity:
PHP Code:
<?php 
require_once(APPPATH.'entities\Folder_entity.php');

class 
User_entity {
 
   public $id;
 
   public $user_name;
 
   public $email;
 
   private $db;

 
   public function __construct($row) {
 
       $this->id $row->id;
 
       $this->user_name $row->user_name;
 
       $this->email $row->email;
 
       $ci = & get_instance();
 
       $this->db $ci->db;
 
   }

 
   public function folders() {
 
      $this->db->select('folders.id,folders.title,folders.access_level',FALSE);
 
      $this->db->from('folders,users',FALSE);
 
      $this->db->where('folders.user_id','users.id',FALSE);

 
      $query $this->db->get();

 
      $results = [];
 
      foreach($query->result() as $row) {
 
           $results[] = new \Folder_entity($row);
 
      }
 
      return $results;
 
   }



scripts stylesheets

$
0
0
Is it a bad practice to make a header and footer file and call EVERY script and style sheet inside ?

New session each refresh

$
0
0
My codeigniter 3.1.10 keeps giving a new session on each refresh nearly. Cannot figure out why but i'm up to like 1.3 million of them now Big Grin

PHP Code:
$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'v_session';
$config['sess_expiration'] = 0;
$config['sess_save_path'] = 'users_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = TRUE

Erro When Connect Database, Please Help, terimakasih

$
0
0
A PHP Error was encountered
Severity: Warning
Message: pg_pconnect(): Unable to connect to PostgreSQL server: fe_sendauth: no password supplied
Filename: postgre/postgre_driver.php
Line Number: 153
Backtrace:
File: C:\xampp\htdocs\www\application\controllers\Home.php
Line: 5
Function: __construct
File: C:\xampp\htdocs\www\index.php
Line: 315
Function: require_once


A Database Error Occurred
Unable to connect to your database server using the provided settings.
Filename: C:/xampp/htdocs/www/system/database/DB_driver.php
Line Number: 436


.png   error_connect.png (Size: 31.39 KB / Downloads: 3)

New Session on every page load CI3.1.10 PHP7.2

$
0
0
So this is a curious one, and I am somewhat confused.

I updated from CI 3.0.4 to CI 3.1.10, and from PHP 5.6 to 7.2.
I am running PHP through IIS fast CSG.

I have two local domains registered in my host file: app.local, test.app.local.

app.local is my Development instance,
test.app.local is my local testing instance which i use before deploying to live testing.

After upgrading, a couple of things broke, which was to be expected. but i got them all sorted out on my Dev instance, and everything worked fine.

I then deployed to my local test site. And what i found was that on every request to the server, a new session was being created.



PHP Code:
$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'appsession-testenv';
$config['sess_expiration'] = 7200;
$config['sess_use_database'] = true;
$config['sess_expire_on_close'] = true;
$config['sess_encrypt_cookie'] = true;
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE

The only change in any configuration is the database credentials, and schema.

I then changed my dev environment to use the same PHP settings and configuration as the test environment, and everything continues to work.


I am hoping some people might have some insight on this??

Role Base Access Control System

$
0
0
I have to refactor old application made in PHP combined with codeigniter version 3.0.6. what is the best solution to implement role base access control system to assign some rules ?  Idea I am new in codeigniter and I will be very thankfull for all suggestions.  Smile

Template recommendation

$
0
0
Hi all,

This will be my 5th project in CI.  All the rest were completely coded by hand but this time around I want to make use of a good "template", if that is the right word for it to kickstart the process.

If you take a look at http://www.kompoz.com this is the basic format I am locking for:   Logo, menu across the top, user notifications and login to the right.  Also a footer.

I will be using bootstrap.  I have seen a fair number of templates there that cost and I've seen on code canyon quite a few as well but I am concerned with those that they are not easily configurable.

For example I found one on code canyon that looked almost idea but the user setup stuff did not look like it could easily be configured to match my needs.  So I'd prefer a "template" rather than a "solution".  I would like to gain the responsive header/menu/footer and user area and I can code the actual user pages and database myself.

But... that being said, if I can get something that is pre-built that I can just modify I am super open to that as well.

Any recommendations?

Mark

[split] Strict typing (Should CI-4 require PHP 7.3 ?)

$
0
0
It would be nice and make applications perform even faster if declare strict_types was used in the CI4 source script.

When strict _types is added numerous errors and warnings are shown resulting in the Php interpretor having to fall back to using the slower Php 4.0 type juggling tests in order to continue processing.

I have used a Php script to add strict_types to every source file and the errors and warnings are easily rectified so why not use this feature... which may be obligatory in future Php versions?

How to setup a route for an optional second param?

$
0
0
Do I need to setup two rules in the Routes.php for these two urls?

Code:
1) http://mydomain.example/show-list
2) http://mydomain.example/show-list/25
PHP Code:
Rule 1$routes->add('show-list''Home::index');
Rule 2$routes->add('show-list/(:any)''Home::index'); 
Is there any way to achieve that in on rule? Just trying to avoid having to repeat it.

PHP Code:
//HomeController.php 
public function index()
{
 
$user_id $this->request->uri->getSegment(2);
 if(
$user_id != NULL) {
 
//filter data by given user_id
 
} else {
 
//show all data
 
}
 return 
view('listdata'compact('data');


If I have only this rule
Code:
$routes->add('show-list', 'Home::index');
then it throws 404 file not found for url http://mydomain.example/show-list/25

If I have only this rule
Code:
$routes->add('show-list/(:any)', 'Home::index');
then it throws 404 file not found for url http://mydomain.example/show-list

SQL Query returns error

$
0
0
Hello, I'm trying to convert the query below into CI's Active Record form, but I get an error :

a.alliance_id could not be bound.

Here is the sql query (when i run it directly to SQL Server works fine)

PHP Code:
SELECT * , a.pledge_ida.ruler_id AS Expr2a.name AS pledge_namea.alliance_id AS Expr4a.challenge_time AS Expr5a.root_name_value AS Expr6a.now_war_id AS Expr7a.oust_time AS Expr8a.skill_level AS Expr9a.castle_id AS Expr10a.agit_id AS Expr11a.rank AS Expr12name_value AS Expr13a.status AS Expr14a.private_flag AS Expr15a.crest_id AS pledge_crest_ida.is_guilty AS Expr17a.dismiss_reserved_time AS Expr18a.alliance_withdraw_time AS Expr19a.alliance_dismiss_time AS Expr20a.alliance_ousted_time AS Expr21b.char_namec.name as ally_namec.crest_id as ally_crest_id
from pledge 
as a
left join user_data 
as b on a.ruler_id=b.char_id
left join alliance 
as c on a.alliance_id=c.id 
order by a
.skill_level desc 

Here is where I got so far. (Maybe I have some syntax error's?)
Thank you in advance!!

PHP Code:
->select("* , 
                a.pledge_id, 
                a.ruler_id AS 'Expr2', 
                a.name AS 'pledge_name', 
                a.alliance_id AS 'Expr4',
                a.challenge_time AS 'Expr5', 
                a.root_name_value AS 'Expr6', 
                a.now_war_id AS 'Expr7',
                a.oust_time AS 'Expr8',
                a.skill_level AS 'Expr9',
                a.castle_id AS 'Expr10',
                a.agit_id AS 'Expr11', 
                a.rank AS 'Expr12', 
                name_value AS 'Expr13', 
                a.status AS 'Expr14', 
                a.private_flag AS 'Expr15', 
                a.crest_id AS 'pledge_crest_id', 
                a.is_guilty AS 'Expr17', 
                a.dismiss_reserved_time AS 'Expr18', 
                a.alliance_withdraw_time AS 'Expr19', 
                a.alliance_dismiss_time AS 'Expr20', 
                a.alliance_ousted_time AS 'Expr21', 
                b.char_name, 
                c.name AS 'ally_name', 
                c.crest_id AS 'ally_crest_id'"
)
 
               ->join('user_data as b''a.ruler_id = b.char_id''left outer')
 
               ->join('Alliance as c''a.alliance_id = c.id''left outer')
 
               ->order_by('a.skill_level''DESC')
 
               ->get('Pledge as a'); 

Give different right for admin cpanel

$
0
0
Hello
For now, in my office our website is http://www.blablabla.com. It uses cpanel in its hosting. Every section in my office has its own folder, so theirs are
http://www.blablabla.com/sectionA
http://www.blablabla.com/sectionB
http://www.blablabla.com/sectionC

Now every admin of each section use cpanel username and password together. Is it possible , as an superadmin IT officer, i create spesific username and password for each section so when admin of section A login he just able to go to folder sectionA, not able to edit folder sectionB or sectionC.

Thanks.

Form Validation CI v3.1.10

$
0
0
Hi Everyone,

I am tinkering on a long term project, and have updated from CI v3.1.9 to v3.1.10.

With v3.1.9, the Form Validation with a callback set in an array returned any processed data set against that $_POSTed value.  That is, the following rule when processed, returned $data set by the function _user_sign_in_check(), when I had a conditional statement equating to be TRUE.  The returned $data was assigned to the $_POSTed 'email_address' variable.


PHP Code:
$this->form_validation->set_rules(
 
 'email_address',
 
 'Email Address',
 
 array(
 
   'trim',
 
   'required',
 
   'valid_email',
 
   array(
 
     'email_address',
 
     array$this->users_mdl'_user_sign_in_check' )
 
   )
 
 ),
 
 array(
 
   'required' => 'You have not provided an <b>%s</b>.',
 
   'valid_email' => 'The <b>%s</b> you have entered is not valid!'
 
 )
); 

With v3.1.10, the Form Validation now just requires to return TRUE for the same conditional check in the function _user_sign_in_check().  When processing the validation which has returned TRUE, (passed), the $_POSTed 'email_address' now holds the actual $_POSTed email address from the form.  If I try to return any processed $data, I have a NULL array.

Has the behavior changed from v3.1.9 to v3.1.10?  I did not mind the callback function returning usable data to the form validation.

I can use the $_POSTed 'email_address' on either side - callback function, or form validation being TRUE - to obtain the User's data, but just curious if this was a change with the v3.1.10 release, as the release notes to not state it explicitly.


Ian.

Problem with pagination that have crud

$
0
0
I follow a tutorial on making a blog application using Codeigniter 3x

here is the link : https://www.youtube.com/watch?v=WoQTjJepDWM

at the end of the tutorial, he shows how to make pagination but it didn't work for me, my other controller work because it didn't have crud, on this controller page that I'm working on right have a crud and it didn't let the pagination work, the limit work, but pagination link didn't show.
I'm using a blog design for my news page.

Here is my controller : 
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

        class News extends CI_Controller {
        function __construct() {
             parent::__construct();
             $this->load->model('news_model');
             $this->load->helper('date');
                 //load pagination library
           $this->load->library('pagination');
        }

            public function index($offset = 0){

        //Pagination Config
        $config['base_url'] =  base_url() . 'news/index/';
        $config['total_row'] =$this->db->count_all('omg_new');
        $config['per_page'] = 2;
        $config['uri_segment'] = 3;
        $config['attributes'] = array('class' => 'pagination-link');
   
        // Init Pagination
        $this->pagination->initialize($config);

        $data['title'] = 'Latest News';

        $data['news'] = $this->news_model->get_posts(FALSE, $config['per_page'], $offset);


    $this->load->view('templates/header');
    $this->load->view('news/index', $data);
    $this->load->view('templates/footer');
    }
    
    public function view($slug = NULL){

        $data['post'] = $this->news_model->get_posts($slug);

            if(empty($data['post'])){
                show_404();
            }

            $data['title'] = $data['post']['title'];


        $this->load->view('templates/header', $data);
        $this->load->view('news/view', $data);
        $this->load->view('templates/footer');


    }

    public function create(){
        $data['title'] = $data['post']['title'];

    $this->load->view('templates/header', $data);
    $this->load->view('news/index', $data);
    $this->load->view('templates/footer');
    }
    

    
}

My model : 

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class News_model extends CI_Model {

    public function __construct()
    {

  $this->load->database();
 
    }

    public function get_posts($slug = FALSE, $limit = FALSE, $offset = FALSE){

    if($limit){
        $this->db->limit($limit, $offset);
    }    

        if($slug === FALSE){
            $this->db->order_by('omg_new.new_id', 'DESC');
            $this->db->join('omg_cat_new','omg_cat_new.cat_new_id = omg_new.cat_new_id');
            $query = $this->db->get('omg_new');
        return $query->result_array();
   }
        
    $query = $this->db->get_where('omg_new', array('slug' => $slug));
            return $query->row_array();

    
    }

}

My index page for news : 

Code:
     <div class="col-md-10 offset-md-1 col-lg-9 offset-lg-0">
       
 
     <div class="section-title">
         <h2><?= $title ?></h2>
         <p></p>
       </div>

 <?php foreach ($news as $post) : ?>

<article>

<div class="col-md-6">
   
       <img src="<?php echo site_url(); ?>assets/upload/<?php echo $post['fea_img']; ?>" alt="">
     
   </div>

<h3><?php echo $post['title']; ?></h3>
<ul class="list-inline">
   <li class="list-inline-item">Categories : <strong><?php echo $post['name']; ?></strong></li>
   <li class="list-inline-item"></li>
 </ul>

<p class=""><?php echo word_limiter($post['content'],40); ?></p>

<a href="<?php echo site_url('/news/'.$post['slug']); ?>" class="btn btn-transparent">Read More</a>

</article>
 <?php endforeach; ?>

 <!-- Pagination -->
 <div class="pagination-links">
       <nav aria-label="Page navigation example">
         <ul class="pagination">
         
          <?php echo $this->pagination->create_links();?>

         </ul>
       </nav>
       </div>
       
</div>

and my view page after clicking read more : 

Code:
  <div class="row">
     <div class="col-md-10 offset-md-1 col-lg-9 offset-lg-0">
       
 
   <article>
       <div class="col-md-6">
      <img src="<?php echo site_url(); ?>assets/upload/<?php echo $post['fea_img']; ?>" alt="">
   </div>
<h3><?php echo $post['title']; ?></h3>
<ul class="list-inline">
   <li class="list-inline-item"></li>
   <li class="list-inline-item"></li>
 </ul>
<p class=""><?php echo $post['content']; ?></p>


</article>
</div>

Can someone help me with this,Sad because I have followed the tutorial correctly but didn't know what was wrong?

controller redirect to method fails

$
0
0
Greetings,  So far I like the concepts and implementation of CI4 - great job!  
However, I can't get the CI3 redirect equivalents to work in CI4.  I'm using the CI4 Beta 4.0.0 - 1, composer installed.

Desired function: Controller needs to pass control to another controller (or method, module, service, etc).  
Purpose: I created a login module because I have 4 applications using similar code.  I will use routes to return control, and encrypted cookies for validations. BUT, perhaps there may be a bug somewhere.  The code below seems too simple to fail.
Huh
Minimalist failing code:
- I added the route: $routes->get('/(:any)', 'Home::$1');
- Modified the canned Home controller to add a dummy method
- Directly calling http://localhost/dummy works just fine.
- redirect ('dummy'); fails, as do all derivative approaches like: route_to('dummy'), header ("location: dummy"); specifying namespaces and home controller ("app\home\dummy"), etc. all fail...

Code follows.  Help anyone??

config/routes:  $routes->get('/(:any)', 'Home::$1');  (and also tried ...->add and other hacks to no avail)

<?php namespace App\Controllers;
use CodeIgniter\Controller;
class Home extends Controller {

public function index() {
  redirect ("dummy");
  echo "didn't route.";
  //return view('welcome_message');
}

public function dummy() {
 echo "Dummy route.";
}
}

- Thanks

Layout question

$
0
0
Hello,

i'm a little suprised by the way the layout system in CI4 works (or maybe i didn't understand it correctly)

As far as i get it from the docs, the view file must declare the layout it is inserted into by:

<?= $this->extend('default') ?>
<?= $this->section('content') ?>
<h1>Hello World!</h1>
<?= $this->endSection() ?>

But why should it be the responsibility of the view file to know that info?
If i go the CI4 approach and want to reuse a page into another layout i have to duplicate it (or tweak it by passing a view name into a variable) + if i want to change the layout for a bunch of page, i have to modify all of them.

I can find my peace with that by tweaking it a little, but i don't see the pros of that design choice ? Any opinion welcomed Smile

Emmanuel

API docs

$
0
0
The CodeIgniter 4 API docs are now online, at https://codeigniter4.github.io/api/

We are using phpDocumentor to generate them, from the documentation blocks in the "system" folder code. Changes should be proposed in the main repo, not in the "api" one. We use an admin script to automate their deployment.

There are a number of errors reported, and the docs don't appear to be complete yet. Please bear with as those are rectified - consider the API docs a work-in-progress for the time being.

codeigniter 3.1.10 index.php code is not concise

$
0
0
codeigniter 3.1.10 index.php code is not concise,duplicate code in many.
$application_folder = strtr(
rtrim($application_folder, '/\\'),
'/\\',
DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR
);

Phpstorm $this->model_name->method() highlighted

$
0
0
Hello, i am new with CodeIgniter so i started to read the docs, specially in the "Model" section there is a code:


$this->load->model('model_name');
$this->model_name->method();



I downloaded Phpstorm Codeigniter plugin and Codeigniter Snippets plugin.But there is still a problem with the code above, when i type:

$this->model_name   - it gets highlighted so it is not found and i cant get autocomplete for the methods in the model class. Should i put some Phpdoc annotation ?
Viewing all 14058 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>