404 page not found Codeigniter Controller - php

I have a simple Codeigniter application.
Its default controller is login. When the application opens in a browser it loads the default controller and its view. But when I'm going to open another controller function, its showing 404 page not found.
This application works fine on my local PC. But on the Client Live Server this problem occurs. Client server is using Ubuntu 12.04.
Sample code
class Login extends CI_Controller{
function __construct(){
parent::__construct();
}
function index(){
$this->load->view('login/index');
}
}
View file:
<table>
<tr>
<td>Username: </td>
<td><?php form_input('txt_username',set_value('txt_username',isset($row->username)?$row->username:''));?> </td>
</tr>
<tr>
<td>Username: </td>
<td><?php form_password('txt_username',set_value('txt_username',isset($row->password)?$row->password:''));?> </td>
</tr>
<tr>
<td colspan=2>for register click
<a href=<?php echo site_url('registers/index');?>>Register</a></td>
</tr>
</table>
Register Controller:
class Registers extends CI_Controller{
function __construct(){
parent::__construct();
}
function index(){
$this->load->view('register/index');
}
}
When I click on Register link or other link in Menu showing 404 not found.But in my pc it is working fine.

Probably your Rewrite rules are not correct.

You can check two options. First: check whether your Rewrite modules is enable or not. Second: check your .htaccess file.
I am strongly recommend to check the second option. Remember your .htaccess file will vary based on hosting server.

In login view you have
<a href=<?php echo site_url('registers/index');?>>Register</a></td>
here link has 'registers' note last 's' and your controller is 'register'
This is a common mistake :)

Related

Creating Module for Admin and Clients with Perfex CRM - Assistance Required

I am working on creating a module for prefex CRM. What this module will be doing is pretty simple, but is quickly turning into more than I anticipated. Allow me to explain what I'm doing and where I'm stuck. I am hoping someone can offer some advice.
Admin View
Admin Module that adds menu item 'Mail' to the side nav bar
When clicking this menu item, a page loads that uses the already uses the drag & drop file upload feature prefex CRM has built in.
When a staff member uploads a file, a modal launches with a form that asks users who the upload should be assigned to. It should use autocomplete to select the customer to attach the file to.
The upload date/time is recorded in database and the record entry in the DB is given a GUID
Client View
Client module that adds menu item 'Mail' to the client navbar
When clicking this menu item, a page loads that displays a table. This table shows all of the entries of file uploads from the admin view. The table should have an image of the scanned mail, the day/time received, and another column that will be blank for now.
So that's what I'm trying to accomplish. In an effort to understand structure of Prefex CRM modules better, I deconstructed the Prefex CRM Employee Chat module. It's hard to compare apples to oranges in this case, but it did help me to develop a file structure which is as follows:
-assets
--css
--js
--module_includes
-controllers
--admin_controller
--client_controller
-models
-uploads
-views
--admin_view
--client_view
mail.php
index.php
install.php
Now that you have an understanding of what I'm doing this is where I need assistance-
Where I'm stuck: I'm looking to recycle the file upload feature that is already built inside of Prefex CRM. Where the file is stored normally is fine and does not need to be changed. I'm unsure of how to reuse these built in functions to create the Mail_ClientsController.php and Mail_Controller.php, as well as some uncertaintly on the mail_admin_view.php, and mail_clients_view.php
Could anyone share some examples of some working modules for Perfex that could shed some light on how to connect these?
mail.php page
<?php
/**
* Ensures that the module init file can't be accessed directly, only within the application.
*/
defined('BASEPATH') or exit('No direct script access allowed');
/*
Module Name: Mail
Description: Client online mailbox module for Perfex CRM system
Version: 2.3.0
Requires at least: 2.3.*
*/
define('BSSI_MAIL_MODULE_NAME', 'bssi_mail');
define('BSSI_MAIL_MODULE_UPLOAD_FOLDER', module_dir_path(BSSI_MAIL_MODULE_NAME, 'uploads'));
$CI = &get_instance();
/**
* Register the activation
*/
register_activation_hook(BSSI_MAIL_MODULE_NAME, 'bssi_mail_activation_hook');
/**
* The activation function
*/
function bssi_mail_activation_hook()
{
require(__DIR__ . '/install.php');
}
/**
* Register new menu item in admin sidebar menu
*/
if (staff_can('view', BSSI_MAIL_MODULE_NAME)) {
if (get_option('bssi_staff_can_delete_messages') == '1') {
$CI->app_menu->add_sidebar_menu_item('bssi_mail', [
'name' => 'BSSI Mail',
'href' => admin_url('bssi_mail/mail_admin_view'),
'icon' => 'fa fa-envelope',
'position' => 6,
]);
}
}
Mail_ClientsController.php (unsure of which functions to put here for upload feature)
<?php defined('BASEPATH') or exit('No direct script access allowed');
class Prchat_ClientsController extends ClientsController
{
}
Mail_Clients_View.php
<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<div class="bssiClient">
<div class="container">
<div class="row">
<div class="col">
<table class="bssiMailTable">
<thead>
<th>Mail Image</th>
<th>Date Received</th>
<th>Actions</th>
</thead>
<tr>
<td>Image Query</td>
<td>Get Date Query</td>
<td>Action Items</td>
</tr>
</table>
</div>
</div>
</div>
</div>
Mail_admin_view.php
<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<div class="bssiAdmin">
<div class="container">
<div class="row">
<div class="col">
<table class="bssiAdminMailTable">
<thead>
<th>Mail Image</th>
<th>Date Received</th>
<th>Actions</th>
</thead>
<tr>
<td>Image Query</td>
<td>Get Date Query</td>
<td>Action Items</td>
</tr>
</table>
</div>
</div>
</div>
</div>
So in your controller/model you would need to load in the standard email model within your construct method. Perfex has a standard model it uses for distributing emails (can be seen under root > application > models > Emails_model.php)
$this->load->model('emails_model');
or if it's being called from outside of the admin controller (e.g. via a cron)
$this->ci->load->model('emails_model');
Within the standard Emails_model.php there is a function "send_simple_email()" which will allow you to send an email to any email address directly from Perfex
E.G.
$email = 'your_email#email.com';
$subject = 'Email Subject';
$message = 'Your Message';
$this->ci->emails_model->send_simple_email($email, $subject, $message);
For file upload, you should be looking at the standard file upload procedure within Perfex (take a look inside upload_helper.php, and you'll find a number of functions which will help you get a file uploaded.

Why the result of laravel eloquent incrementing always?

I try to execute select * from articles where category_id=1 in laravel eloquent model as below.
$articles = Article::where('category_id', '=', $id)->get();
And i pass this to my view file by using compact method as below.
return view('homeview.index', compact('articles'));
And i use this $articles variable in foreach loop and print each article's title in view file. But the thing is when i try to execute this, my view file is refreshing continuously and incrementing the same div class which i used to print the article titles. I tried to use above code as raw query also. It also generates the same issue.
below is my view file.
<tbody>
#foreach($articles as $article)
<tr>
<td><img src="{{asset('/img_thumbs/'.$article->img_thumb)}}"></td>
<td class="-align-center">
<h4>{{$article->title}}</h4>
<p>{{$article->sub_paragraph}}</p>
</td>
<tr>
#endforeach
</tbody>
Please help me to solve this.
The bottom line is, THere's an error in your HTML.
You are opening <tr> but not closing it.
<tbody>
#foreach($articles as $article)
<tr>
<td><img src="{{asset('/img_thumbs/'.$article->img_thumb)}}"></td>
<td class="-align-center">
<h4>{{$article->title}}</h4>
<p>{{$article->sub_paragraph}}</p>
</td>
</tr> <!-- HERE -->
#endforeach
</tbody>
i used some java scripts for my application. That repeating thing happening because one script file. After i comment it, my page is working fine. I used some template called inspinia and it is happening because inspinia.js file.

Codeigniter : Protect folder from direct access

Hello guys I have a small project connected with a database.I own a function for
uploading files into a folder and also save files path to the database.
In my index page I read files path from database and output a table with links to these files for downloading.
Everything works fine and files are able to be downloaded unless,
the problem is that I forgot to secure this folder and yesterday realized that I should protect it somehow because people can download files directly with links
and I need to check if user is logged to be able to download it.
So my question is:
How to protect the folder with these files from direct access and make only logged users
to be able to download files from this folder
My upload path is ./uploads/ inside this folder I had htaccess file
order deny,allow
deny from all
In controller I have
public function viewAllPersons()
{
if($this->ion_auth->logged_in())
{
if(!$this->ion_auth->in_group(1)){
show_404();
}
else {
$data = array();
$data['persons'] = $this->get_persons(); // get all persons from database as array
$this->load->view('admin/header_view');
$this->load->view('admin/persons_view',$data); // pass data to the view
$this->load->view('admin/footer_view');
}
} else {
redirect(base_url());
}
}
My view file contains this
{
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
<h1 class="page-header"><span class="glyphicon glyphicon-th-list"></span> Archive</h1>
<h2 class="sub-header pull-left">All records db</h2>
<span class="glyphicon glyphicon-plus-sign"></span> Add new record
<form class="navbar-form navbar-right" method="post" action="<?=site_url('dashboard/persons');?>" name="searchform" >
<div class="form-group">
<input type="text" id="search" name="search" class="form-control" placeholder="" autocomplete="off">
</div>
<button type="submit" class="btn btn-default"><span class="glyphicon glyphicon-search"></span></button>
</form>
<div class="clearfix"></div>
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Firstname</th>
<th>Middlename</th>
<th>Lastname</th>
<th>ID</th>
<th>Filename</th>
<th>Data</th>
<th>Options</th>
</tr>
</thead>
<tbody>
<?php
$counter = 1;
foreach($persons as $person) { ?>
<tr>
<td><?=$person['person_id'];?></td>
<td><?=$person['first_name'];?></a></td>
<td><?=$person['middle_name'];?></td>
<td><?=$person['last_name'];?></td>
<td><?=$person['personal_number'];?></td>
<td><?=$person['document_path'];?></td> <!-- show links to files for each row !-->
<td><?=$person['created_on'];?></td>
<td>
<a href="<?=base_url('/dashboard/persons/edit/'.$person['person_id'])?>">
<span class="glyphicon glyphicon-pencil edit-icon"></span>
</a>
</td>
</tr>
<?php $counter++; } ?>
</tbody>
</table>
<div class="row">
<?php if(isset($links)) {
echo $links;
}?>
</div>
</div>
</div>
<?php } ?> }
Here is my problem when I output links to files I need to check if user is logged before to be able to download files using the links saved in database
Example of project -- picture
Because people can download files with direct link
Example
http://website.com/uploads/document.docx
Once I have htaccess file I`m unable to download a file need maybe a function to rewrite rules or
to give access to logged users somehow
I also tried a download helper from codeigniter but download files only if I remove the htaccess rules If rules exist inside htaccess file download helper is unable to download the file.
Thanks in Advance!
You have a few possibilities, the simplest one would be to have control of the access of these folders with a Controller.
You need a file table with minimum : id, path.
Let's say user A wants file ABC.jpg (with the id of 1337) : Instead of serving him http://example.com/uploads/ABC.jpg, you give him http://example.com/files?id=1337.
This route calls the index of controller Files and in the index you can do this pseudo-code :
function index() {
//Check if logged in
if (!$user->logged()) {
redirect('/404');
}
//Get file from database
$file = $this->db->query('SELECT * FROM file WHERE id='.$this->input->get("id"))->result();
//Then serve the file
header("Content-type: image/jpeg");
readfile($file->path);
}
EDIT :
I will try to explain it in other terms :
Create a table of uploaded files called file. Something like
CREATE TABLE file (id INT NOT NULL AUTO_INCREMENT, path VARCHAR(100) NOT NULL, PRIMARY KEY ( id ))
Update your person table so that it doesn't have the document_path but file_id instead. And when you upload a file, you save it's path in the table file then assign the file id to the person (in table person of course).
Instead of <?php echo base_url('/uploads/'.$person["document_path"]);?>, you need to do <?php echo base_url('/file?id='.$pseron['file_id']);?>
This is because we want the user to go to a specific controller that you need to create File.php (the controller).
In this controller, the function index (the default function of the controller File) must do something like I showed you before the edit. It means you retrieve from the database the file path based from the file id, then you serve the file. This is called PHP serving instead of Apache (default) serving of files.
I was facing the same problem because my files were directly accessed by the users. especially uploads files and images. I was working on a PHP framework.
I simply added one line of code in the .htaccess file. and it was done.
If you don't have one in your WEBSITE FOLDER then create a .txt file and name it as .htaccess (if you are not using any framework).
then open the .htccess file and simply write: Options -Indexes
NOTE: DON'T PUT .htaccess file in any folder just simply open your website folder in htdocs and create .htaccess

Zend Framework 2 - Get content from other module

My project looks like this:
- root<br>
- module
- Application
- view
- application
- index
- index.phtml
- ZfcUser
- view
- zfc-user
- user
- login.phtml
(Sorry if isn't readable it's my first post I tried my best)
When I go to (from my web browser) http://site.local/user and I'm authenticated
ZfcUser display login.phtml
Now I want to show some information coming from my module application but I don't want to write specific code on login.phtml or even on any ZfcUser controller since I think it isn't a good practice.
The good practice(I guess) should be to have a prepared controller on application where I give some content to the caller.
Can I get that directly from login.phtml?
Sorry for my English. It isn't my main language if you have trouble understanding my problem feel free to reply and asking for more clarification.
I'm looking for the best method to do this to really understand how it have to be done.
Thanks for paying attention to my problem.
Thanks for the grammar correction.
Since I got some negative feedbacks (-3) I post my solution here maybe if someone got the same problem it will be helpful...
I have put on the login.phtml something like:
<?php
use Application\Controller\News
$news = new News();<br>
$newsContent = $news->getNewsTable();
?>
Then I can use the result from this function on my view.
It works and I don't think it's a good practice and should be avoided but at least it works.... :|
Here is my solution (The previous one was wrong but I let it for informal/logical purpose about the answers I got)
Instead of going into ZfcUser after login I have route the request to ZfcDataGrid.
To do that simply edit the UserController.php on ZfcUser/Controller/
and modify indexAction to looks like:
if (!$this->zfcUserAuthentication()->hasIdentity()) {
return $this->redirect()->toRoute('zfcuser/login');
}
return $this->redirect()->toRoute('ZfcDatagrid');
Here we check if we are authenticated, if we are we go to ZfcDataGrid, if not we go to default zfcuser login.
On my Data file I used this: (on ZfcDataGrid Data file provides the content used to render the grid)
$sm = $this->getServiceLocator();
$this->newsTable = $sm->get('Application\Model\News');
$news = $this->newsTable->fetchAllToArray();
foreach ($news as $index=>$value) {
$data[$index] = $value;
}
return $data;
Then on my .phtml
<div name="zfcuser" style="position:relative; top:10px; left:80%;">
<div style="float:left; padding-right:16px;">
<?php echo $this->gravatar($this->zfcUserIdentity()->getEmail()) ?></div>
<h3><?php echo $this->zfcUserDisplayName() ?>!</h3>
[Sign Out]
<div style="clear:both;"></div>
</div>
<a class="btn btn-primary" href="<?php echo $this->url('ZfcDatagrid') ?>"
<?php echo $this->translate('cancelback') ?>
role="button" class="btn" data-toggle="modal">Cancel and back to previous page</a>
<?php
echo '
<h2>Title : ' . $new[0]['title'] . '</h2>'; ?>
<form method="post" action="somepage">
<textarea name="content" style="width:100%">
<?php echo '<p>' . $new[0]['content'] . '</p>
'?></textarea>
</form><a class="btn btn-primary" href="<?php echo
$this->url("news")."save";?>">
<?php echo $this->translate('Validate'); ?>
</a>
So the answer I found it's to use model you need on the other module controller (use Application\Model\NewsModel;)
Then on the controller get the data from your model and pass the results to your view.
Then on your view show the data.
If someone can tell me if it's the right way to do what I wanted or if needed more explanation feel free to ask.
Thanks again for your help Carlos, I really appreciate it.
Happy new year :D

PHP- Zend Framework- Call action from another action's view on CentOS Server

I deploying my PHP project on CentOS and i using Zend Framework.
I have problem:
When i call an action from another action's view and it displayed is good on Windows Server but not good on CentOS Server. It's not load info in head tag when view source html.
Example:
In file: index.phtml (for index action in Product controller)
<div class="left">
<?php echo $this->action('left', $this->controller,'product',array('currentModule'=>$this->module)); ?>
</div>
<div class='right'>
<?php echo $this->action('list', $this->controller, 'product',array('currentModule'=>$this->module,'back'=> $this->back,'page'=> $this->page)); ?>
</div>
So, we can see. In index.phtml, i called 2 another action (left action and list action in Product controller). Windows is OK but on CentOS, this code is not work.
Please help me for it run on CentOS.
When i was try remove 2 this action:
<div class="left">
//code
</div>
<div class='right'>
//code
</div>
So, it's woking on CentOS :)
Thanks!
Windows migrations to unix based systems usually run into problems like this when you have case sensitivity issues - windows doesn't require case to be correct but unix does. Make sure your folders and php file names have the correct case and try it again - if not give us some of the errors your encountering and we could help more!
By the way, a little off topic here but the action view helper is not efficient, I would suggest looking into custom view helpers and the render view helper instead.

Categories