I am getting a URL not found error in my MVC Php application. The .htacess file seems fine and apache is configured well because the other application runs well. Am hosting my mysql db on amazon.
here is my code snippet.
LoginForm.php
/**
*
*/
class LoginForm extends Controller {
public $model;
public function index() {
//check if they are already logged in
if (!isset($_SESSION['email'])) {
require 'application/views/login/index.php';
} else {
//redirect to admin data
header("Location:" . URL . "home");
}
}
public function login() {
// get the post
$this->model = $this->loadModel('login');
if (isset($_POST['email']) && isset($_POST['password']) && isset($_POST['country'])) {
// echo "priv_".$_POST["country"];
$validate = $this->model->validate($_POST['email'], MD5($_POST['password']), $_POST["country"], 1);
if ($validate != 0) {
// get all the data
$data = $this->model->getByID($_POST['email']);
/*
echo "<pre>";
var_dump($data);
echo "</pre>";
exit();
*/
// set the session
session_start();
$_SESSION['email'] = $_POST['email'];
/*
* Privilege Sesssion settings Start
*
*/
$_SESSION["pnya"] = $data[0]['pnya'];
/*
* Privilege Session End
*/
header("Location:" . URL . "home");
} else {
header("Location:" . URL . "LoginForm");
}
} else {
// #todo reload login page page
header("Location:" . URL . "LoginForm");
// #todo wth appropriate errors
}
// use php to check if its an email
// if not set the errors
// #todo use model to get dta a from staff and validate
// #todo if it all succeeds then rdirect
}
public function logout() {
// destroy the session
session_start();
session_destroy();
// redirect to login page
header("Location:" . URL . "LoginForm");
}
}
?>
.htacess
Options -MultiViews
RewriteEngine On
Options -Indexes
RewriteBase /MIS/ysw/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
config.php
error_reporting(E_ALL);
ini_set("display_errors", 1);
define('URL', 'http://42.11.223.45/MIS/ysw/');
define('DB_TYPE', 'mysql');
define('DB_HOST', 'XXX');
define('DB_NAME', 'amazon');
define('DB_USER', 'xxxx');
define('DB_PASS', 'ddddd');
I have spent close to 13hrs trying to figure out what I have not done right, but will be glad if informed
Please check if mod_rewrite is working, you can take help from here: https://docs.bolt.cm/3.0/howto/making-sure-htaccess-works
Related
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/" />
`
Once my users log in and verify themselves I am sending my adimninstrators to an activity page. This inital page shows session values when I dump the sessions. However when I move to any other page in my site I lose the session values.
I am setting my pages by including the classes in the header file. I calling session start (if (session_id() === "") { session_start(); }) at the top of each file in the site. I have nothing within my code that will cause my session to be destroyed.
when the user is being authenticated, i am using this snippet of PHP
<?php require_once('includes/init.php'); ?>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['login'])) {
require 'auth_login.php';
} elseif (isset($_POST['register'])) {
require 'register.php';
}
}
?>
when they submit the form, they are sent to auth_login, and the successful login/verification code looks like this:
if ($_POST['code'] == $users->auth_code) {
$_SESSION['logged_in'] = 'true';
$_SESSION['id'] = $users->id;
redirect("/admin/activity.php");
}
This is the content of my init file:
if (session_id() === "") { session_start(); }
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once("functions.php");
require_once("config.php");
require_once("database.php");
require_once("db_object.php");
My functions file:
<?php
function classAutoLoader($class){
$class = strtolower($class);
$the_path = "includes/{$class}.php";
if(is_file($the_path) && !class_exists($class)){
require_once($the_path);
} else {
die ("File, {$class} does not exist");
}
}
spl_autoload_register('classAutoLoader');
function redirect($location){
header("Location:{$location}");
}
?>
My config file:
<?php
define('DB_HOST', 'XXX');
define('DB_USER', 'XXX');
define('DB_PASS', 'XXX');
define('DB_NAME', 'XXX');
?>
My database class
class Database{
public $connection;
function __construct(){
$this->open_db_connection();
}
public function open_db_connection(){
$this->connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if ($this->connection->connect_errno){
die("Database connection failed ". $this->connection->connect_errno);
}
}
public function query($sql){
$result = $this->connection->query($sql);
$this->confirm_query($result);
return $result;
}
private function confirm_query($result){
if (!$result){
die("Query Failed". $this->connection->error);
}
}
public function escape_string($string){
$escaped_string = $this->connection->real_escape_string($string);
return $escaped_string;
}
public function the_insert_id(){
return $this->connection->insert_id;
}
}
$database = new Database();
When my user lands on activity.php, i am using these 2 snippets
echo '<pre>';
var_dump(session_status());
echo '</pre>';
echo '<pre>';
var_dump($_SESSION);
echo '</pre>';
There I get my 2 values in the session dump and I get int(2) session status. When i hit any other page in my directory I lose the sessions in the dump, but i still see a int(2) for my status.
I am leaning towards thinking that my issue exist within the htaccess file.
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/(admin|processes|paypal-ipn/)$
RewriteRule ^.*$ ./index.php
All traffic except a directory is having the file extensions striped. Could that be what is causing my problem.
Thanks in advance.
Work In Progress:
I'm afraid I'm going to have to go, but we have proven that the session saves correctly and can be read from the same file, as is written to. So if the other files are in another folder then there may be some folder specific PHP.ini settings messing up the session reading? I think the issue does appear to be outside the scope of what you've put in your question, but that is not to place blame, simply that it's more complex to establish what's going on.
I have nothing within my code that will cause my session to be destroyed.
Then this means that sessions are not being started, so the session data is unavailable to PHP.
I do have session start at the top of my init file.
So the next step is to var_dump($_SESSION); immediately after you start the session, like so:
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);
var_dump($_SESSION);
require_once("functions.php");
...
And let us know what this outputs? Are the sessions empty or are they full of stuff at the point of time the page loads?
I do not know how to access the sessions directory on my server
You need to load the phpinfo or the php.ini file and see where it says:
session.savepath . This is where sessions are stored. Once you know this you can open that folder, and find the session file, usually sess_<session_id value> and see if any data is being written to the session.
Update 3:
Please try this adjusted code:
if ($_POST['code'] == $users->auth_code) {
$_SESSION['logged_in'] = 'true';
$_SESSION['id'] = $users->id;
session_write_close();
session_start();
error_log(print_r($_SESSION,true));
redirect("/admin/activity.php");
This will write to the session, close the session and then open the session and save the contents to the Error Log file. You should be using error logs far more than screen displays.
Result:
The error log shows me this. [02-Aug-2019 21:10:50 UTC] Array ( [logged_in] => true [id] => 1 ) [02-Aug-2019 21:10:50 UTC] PHP Notice: Undefined index: email in /home/linepickleadmin/public_html/admin/auth_verify.php on line 34
I have a CodeIgniter project on a subdomain. Now, When I visit sub.example.com it loads a login page and on successful login, it redirects to dashboard. Once logged in and session are in place visiting sub.example.com/login/ will auto-redirect to the dashboard page. Now here's my problem. After successful login, visiting sub.example.com doesn't redirect anywhere it simply load the login page. But visiting sub.example.com/index.php does redirect me to the dashboard page.
For some reason, my index method is called or working properly.
Here my code.
.htaccess
IndexIgnore *
php_value date.timezone Asia/Kolkata
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
## Remove www from URL
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ https://sub.example.com/$1 [R=301,L]
## Redirect to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^sub.example.com$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
config.php
if ($_SERVER['REMOTE_ADDR'] == "127.0.0.1") {
$config['base_url'] = "http://" . $_SERVER['SERVER_NAME'];
} else {
$config['base_url'] = "https://" . $_SERVER['SERVER_NAME'];
}
routes.php
$route['default_controller'] = 'root';
$route['root_controller'] = 'root';
/*API CONTROLLER*/
$route['api_controller'] = 'api';
/*Guest Controller*/
$route['guest_controller'] = 'guest';
/*Custom Routes*/
$route['dashboard/(:any)'] = 'root/dashboard/$1';
$route['search/(:any)'] = 'root/search/$1';
$route['search/(:any)/(:num)'] = 'root/search/$1/$2';
$route['export/(:any)'] = 'root/export/$1';
root controller
public function __construct()
{
parent::__construct();
$this->default_data = array("project_name" => PROJECT_NAME);
if ($this->router->method == "privacy_policy") {
return;
}
$this->load->library('session');
if ($this->router->method == "login") {
if ($this->session->userdata("username")) {
redirect(base_url("dashboard/"));
}
} else {
if (!$this->session->userdata("username")) {
redirect(base_url("login/"));
}
}
//Set MYSQL timezone
$this->db->query("SET time_zone = '+05:30'");
}
/**
* Dashboard View
*/
public function index()
{
redirect(base_url("/dashboard/"));
}
/**
* Login View
*/
public function login()
{
$data = $this->default_data;
if ($_POST) {
$username = $this->input->post("username");
$plain_password = $this->input->post("password");
$this->load->model("authenticate");
if (!$this->authenticate->auth($username, $plain_password)) {
$data['message'] = "Invalid Credentials";
}
}
$this->load->view('login', $data);
}
Update
Forgot to mention that I am having this issue only on the remote server. On localhost it's working fine.
Hers is an idea - add this after you have loaded the session object:
if ($this->session->userdata("username") && $this->router->method == "index") {
redirect(base_url("dashboard/"));
}
Please use this .htaccess
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
if you visiting sub.example.com/index.php successfully then try this
public function login()
{
$data = $this->default_data;
if ($_POST) {
$username = $this->input->post("username");
$plain_password = $this->input->post("password");
$this->load->model("authenticate");
if (!$this->authenticate->auth($username, $plain_password)) {
$data['message'] = "Invalid Credentials";
}else{
//go to dashboard function
$this->index();
}
}
$this->load->view('login', $data);
}
Turns out there was some issue with files on the remote server. I don't exactly know if the files were different or corrupted but replacing the whole project on the remote server fixed the issue
"Turns out there was some issue with files on the remote server. I don't exactly know if the files were different or corrupted but replacing the whole project on the remote server fixed the issue" --
#Akash You should compare your local and remote .htaccess files. I think that will explain your problem.
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');
}
}
}
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