404 page not found - php

I'm trying to figure out why I'm getting a 404 page now found when I load a page. I thought it was because of the file structure but now i'm not sure why.
myurl.com/mycms/modules/bios/quotes
Here's the file structure
root
root/mycms
root/mycms/application
root/mycms/application/modules
root/mycms/application/modules/bios/
root/mycms/application/modules/bios/controllers/quotes
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Quotes extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
//Config Defaults Start
$msgBoxMsgs = array(); //msgType = dl, info, warn, note, msg
$cssPageAddons = ''; //If you have extra CSS for this view append it here
$jsPageAddons = ''; //If you have extra JS for this view append it here
$metaAddons = ''; //Sometimes there is a need for additional Meta Data such in the case of Facebook addon's
$siteTitle = ''; //alter only if you need something other than the default for this view.
//Config Defaults Start
//examples of how to use the message box system (css not included).
//$msgBoxMsgs[] = array('msgType' => 'dl', 'theMsg' => 'This is a Blank Message Box...');
/**********************************************************Your Coding Logic Here, Start*/
// Checks to see if a session is active for user and shows corresponding view page
if ($this->kowauth->isLoggedIn())
{
$bodyContent = "testing"; //which view file
}
else
{
redirect('login', 'refresh');
}
$bodyType = "full"; //type of template
/***********************************************************Your Coding Logic Here, End*/
//Double checks if any default variables have been changed, Start.
//If msgBoxMsgs array has anything in it, if so displays it in view, else does nothing.
if (count($msgBoxMsgs) !== 0)
{
$msgBoxes = $this->msgboxes->buildMsgBoxesOutput(array('display' => 'show',
'msgs' => $msgBoxMsgs));
}
else
{
$msgBoxes = array('display' => 'none');
}
if ($siteTitle == '')
{
$siteTitle = $this->metatags->SiteTitle(); //reads
}
//Double checks if any default variables have been changed, End.
$this->data['msgBoxes'] = $msgBoxes;
$this->data['cssPageAddons'] = $cssPageAddons; //if there is any additional CSS to add from above Variable this will send it to the view.
$this->data['jsPageAddons'] = $jsPageAddons; //if there is any addictional JS to add from the above variable this will send it to the view.
$this->data['metaAddons'] = $metaAddons; //if there is any addictional meta data to add from the above variable this will send it to the view.
$this->data['pageMetaTags'] = $this->metatags->MetaTags(); //defaults can be changed via models/metatags.php
$this->data['siteTitle'] = $siteTitle; //defaults can be changed via models/metatags.php
$this->data['bodyType'] = $bodyType;
$this->data['bodyContent'] = $bodyContent;
$this->data['userData'] = $this->users->getUserByUserID($this->session->userdata('userID'));
$roster = $this->kowauth->getRosterList($this->data['userData']->usersRolesID);
$userRoster = array();
foreach ($roster AS $member)
{
$userRoster[$member->id] = $member->rosterName;
}
$this->data['userRoster'] = $userRoster;
$this->data['personalMessages'] = array($this->pmmodel->
getInboxUnreadMessagesCount($this->session->userdata('userID')), $this->pmmodel->
getInboxMessagesCount($this->session->userdata('userID')), $this->pmmodel->
getLast5Messages($this->session->userdata('userID')));
$this->load->view('cpanel/index', $this->data);
}
}
/* End of file quotes.php */
/* Location: ./application/modules/bios/controllers/quotes.php */
htacess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteEngine On
RewriteBase /kowmanager
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
I don't have any custom routes.

Usually when using modules with CI you don't need to include the word "modules" in your URL. Try accessing myurl.com/mycms/bios/quotes
It might be that your cms works in a different way, but generally the theory is that any controllers in your modules are accessed simply by using the module name and then the controller name. If you think about using CI without modules you wouldn't use the word "controllers" in the URL.

Related

pretty url static files not loading

I'm building a social network and my htaccess pretty url isn't loading the css/js/img/php files. The following rules work
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^login$ index.php?act=login [NC]
RewriteRule ^register$ index.php?act=register [NC]
RewriteRule ^forgot$ index.php?act=forgot [NC]
But the following rule works but doesn't load the css/js/img
RewriteRule ^user/(\w+)/?$ index.php?act=profile&user=$1 [NC]
What I'm I doing wrong? I don't have a base url set. The home page url is
http://localhost/myWebsite/source/
I also have the following logic in my controller which is included at the top of the index page
if ($isLogged == true) {
// check if page request set
if (isset($_GET['act'])) {
// valid pages
$pgeArray = array("stories", "hashtags", "media", "user", "search");
// check if page is valid
if (in_array($_GET['act'], $pgeArray)) {
// get page name request
$pgeReq = $_GET['act'];
}else {
// show registration page
$pgeReq = "home";
}
}else {
// show registration page
$pgeReq = "home";
}
}
else {
// check if page request set
if (isset($_GET['act'])) {
// valid pages
$pgeArray = array("register", "login", "forgot");
// check if page is valid
if (in_array($_GET['act'], $pgeArray)) {
// get page name request
$pgeReq = $_GET['act'];
}else {
// show registration page
$pgeReq = "login";
}
}else {
// show registration page
$pgeReq = "login";
}
}
All I had to do is specify a base url <base href="http://localhost/myWebsite/source/" />
`

page redirect issue codeigniter

I am unable to redirect to page using the redirect() in codeigniter. Also the same problem if I try to use location.href within view. It just redirects me to the page without the css and js includes
mY CONFIG
$config['base_url'] = 'http://localhost/tsb/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI'; //I have switched the 3 protocols too
HTACCESS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
BOOK CONTROLLER
public function psg_sel()
{
$book_name = $this->input->post('book_name');
$book_id = $this->cj_model->get_book_id($book_name);
$ch_id = $this->input->post('chapter_id');
$cj_mask = $this->input->post('cj_mask');
if($cj_mask == ''){
$psg_sel = $this->cj_model->psg_sel($book_id, $ch_id);
if($psg_sel === true){
redirect(base_url() . 'passage/', 'refresh');
}
}
}
PASSAGE CONTROLLER
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Passage extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->database();
}
public function index()
{
$data['page_name'] = 'passage';
$this->load->view('pages/index', $data);
}
}
Please help I dont know whats going wrong. I can access http://localhost/tsb/passage/ directly from address bar but none of these will work properly location.href="passage/";or redirect(base_url() . 'passage/', 'refresh'); This is how it displays
At controller try this code. First load url helper then try..
The redirect statement in code igniter sends the user to the specified web page using a redirect header statement.This statement resides in the URL helper which is loaded in the following way:
$this->load->helper('url'); //loads url helper
Controller:
public function psg_sel()
{
$this->load->helper('url'); //loads url helper
$book_name = $this->input->post('book_name');
$book_id = $this->cj_model->get_book_id($book_name);
$ch_id = $this->input->post('chapter_id');
$cj_mask = $this->input->post('cj_mask');
if($cj_mask == ''){
$psg_sel = $this->cj_model->psg_sel($book_id, $ch_id);
if($psg_sel === true){
redirect(base_url('passage'), 'refresh');
}
}
}

Codeigniter form_open 404

After searching for hours I still don't get why I get a 404 error when I do a form_submit.
My controller looks like this:
class pages extends CI_Controller{
function view($page = 'home'){
$this->load->helper('url');
if(!file_exists('application/views/pages/'.$page.'.php')){
show_404();
}
$data['title'] = $page;
$this->load->view('templates/view',$data);
}
public function login_validation(){
}
The view page:
<?php echo form_open('pages/login_validation');
echo form_input(array(
'name' => 'firstname',
'placeholder' => 'Voornaam',
'class' => 'form-control input-lg',
'tapindex' => '1'
));
echo form_submit(array(
'name' => 'login_submit',
'value' => 'Register',
'class' => 'btn btn-primary'
));?>
.htaccess (in the root folder):
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|indexcp\.php?|resources|robots\.txt)
RewriteRule ^([A-Za-z0-9_/.-]+)$ index.php?/$1
I have this line in the autoload.php:
$autoload['helper'] = array('form','url');
In config.php:
$config['base_url'] = 'http://localhost/';
$config['index_page'] = '';
routes.php:
$route['(:any)'] = "pages/view/$1";
$route['default_controller'] = "pages/view";
$route['404_override'] = '';
What is wrong with this configuration?
The problem you are having is with the following route (I am assuming you've posted the whole of your routes file):
$route['(:any)'] = "pages/view/$1";
This means that any file you load is going to go to the pages controller, call the function view, and pass in the parameter which is as $1. In this case, it's going to pass through either 'pages/login_validation' or 'login_validation' (I'm not 100% sure which, and I can't test at the moment.
Your view function is checking to see if the file 'application/view/pages/login_validation.php' or 'application/view/pages/pages/login_validation.php' exists, and when it can't find it, it is giving the 404 page.
To fix this, you can add the following into the routes.php file (above the $route[(:any)] = 'pages/view/$1'; line):
$route['pages/login_validation'] = 'pages/login_validation';
That will explicitly check that the page wanting to be loaded is the login_validation one, and then call that function within the pages controller.
If you don't want to keep adding routes each time, then you could modify the view function in pages to check if the $page variable matches the name of a function, and if so then call that function:
if (function_exists($page))
{
$page();
}
The trouble you might have then is that you won't easily be able to pass through additional parameters to the given page. You might also accidentally name a page the same as a function in PHP, and then it will try and call that instead of loading the page you intend.

Unable to redirect to a new page - CodeIgniter

I have a login system that works, but on the redirect function it gives me the error The requested URL "http://localhost/musiclear/index.php/welcome" cannot be found or is not available. Please check the spelling or try again later.
This is where I am using it (login.php):
function validate_credentials() {
$this->load->model('membership_model');
$query = $this->membership_model->validate();
if ($query) { // if users credentials validated
$data = array('usernames' => $this->input->post('username'),
'is_logged_in' => true);
$this->session->set_userdata($data); //set session data
redirect('welcome', 'refresh'); //redirect to home page
} else { //incorrect username or password
$this->index();
}
}
This is where I am directing it to (welcome.php):
class Welcome extends CI_Controller {
public function index() {
$this->home();
}
public function home() {
$this->load->model('model_users');
$data['title'] = 'MVC Cool Title'; // $title
$data['page_header'] = 'Intro to MVC Design';
$data['firstnames'] = $this->model_users->getFirstNames();
// just stored the array of objects into $data['firstnames] it will be accessible in the views as $firstnames
$data['users'] = $this->model_users->getUsers();
$this->load->view('home_view', $data);
}
}
Im thinking it is something wrong with the path, or where its linking to but im not sure. This is my directory setup:
Can someone please tell me whats wrong and how I can make it link to my page? Thanks so much
Have you created the .htaccess in your application folder?
Maybe this can work for your project:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /musiclear/index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /musiclear/index.php
</IfModule>
You are welcome

Facebook Like Custom Profile URL PHP

Making a website and I want to put in a custom profile URL for all the users on my site (like facebook).
On my website already, people have a page like http://sitename.com/profile.php?id=100224232
However, I want to make a mirror for those pages that relates to their username. For example, if you go to http://sitename.com/profile.php?id=100224232 it redirects to you http://sitename.com/myprofile
How would I go about doing this with PHP and Apache?
No folders, no index.php
Just take a look at this tutorial.
Edit :
This is just a summary.
0) Context
I'll assume that we want the following URLs :
http://example.com/profile/userid (get a profile by the ID)
http://example.com/profile/username (get a profile by the username)
http://example.com/myprofile (get the profile of the currently logged-in user)
1) .htaccess
Create a .htaccess file in the root folder or update the existing one :
Options +FollowSymLinks
# Turn on the RewriteEngine
RewriteEngine On
# Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php
What does that do ?
If the request is for a real directory or file (one that exists on the server), index.php isn't served, else every url is redirected to index.php.
2) index.php
Now, we want to know what action to trigger, so we need to read the URL :
In index.php :
// index.php
// This is necessary when index.php is not in the root folder, but in some subfolder...
// We compare $requestURL and $scriptName to remove the inappropriate values
$requestURI = explode(‘/’, $_SERVER[‘REQUEST_URI’]);
$scriptName = explode(‘/’,$_SERVER[‘SCRIPT_NAME’]);
for ($i= 0; $i < sizeof($scriptName); $i++)
{
if ($requestURI[$i] == $scriptName[$i])
{
unset($requestURI[$i]);
}
}
$command = array_values($requestURI);
With the url http://example.com/profile/19837, $command would contain :
$command = array(
[0] => 'profile',
[1] => 19837,
[2] => ,
)
Now, we have to dispatch the URLs. We add this in the index.php :
// index.php
require_once("profile.php"); // We need this file
switch($command[0])
{
case ‘profile’ :
// We run the profile function from the profile.php file.
profile($command([1]);
break;
case ‘myprofile’ :
// We run the myProfile function from the profile.php file.
myProfile();
break;
default:
// Wrong page ! You could also redirect to your custom 404 page.
echo "404 Error : wrong page.";
break;
}
2) profile.php
Now in the profile.php file, we should have something like this :
// profile.php
function profile($chars)
{
// We check if $chars is an Integer (ie. an ID) or a String (ie. a potential username)
if (is_int($chars)) {
$id = $chars;
// Do the SQL to get the $user from his ID
// ........
} else {
$username = mysqli_real_escape_string($char);
// Do the SQL to get the $user from his username
// ...........
}
// Render your view with the $user variable
// .........
}
function myProfile()
{
// Get the currently logged-in user ID from the session :
$id = ....
// Run the above function :
profile($id);
}
To conclude
I wish I was clear enough. I know this code is not pretty, and not in an OOP style, but it could give some ideas...

Categories