Codeigniter base_url() in whack? - codeigniter

I was trying to set up a basic log in form with a following code:
<?=form_open(base_url() . 'main/login'); ?>
However after submitting the form the url shows this:
example.com/main/http//example.com/http//example.com/main/login
So I guess in essence for some reason the base-url is printed twice before the controller/method declaration. If I clear the base url value in my config file then the application works normally. I am however curious on what could cause this. For additional information I am working on xampp with a virtualhost and I have mod-rewrite on with a .htaccess file located at the document root.

CodeIgniter automatically adds the base_url to the action of the form when you use the form helper.
For example, you can use:
<?=form_open('main/login'); ?>
which will produce:
http//example.com/main/login
And a correct URL! Pretty simple! :D
More information at:
http://codeigniter.com/user_guide/helpers/form_helper.html

The file config.php under application/config has the setting:
$config['base_url'] = '';
Give it the folder/directory path. For example:
$config['base_url'] = 'http://localhost/ci_test/';
Don't forget to mention the protocol (http://). Alternatively try the site_url() method instead of base_url() for form opening. Skip it if using the form_open() function:
<form action="<?php echo site_url('main/login'); ?>"> ... </form>
Or
<?php form_open('main/login'); ?>
For more help: http://codeigniter.com/user_guide/helpers/url_helper.html
Not sure about the .htaccess file you have used. But this might be the answer codeigniter: why is that when i echo base_url() in an href attribute of an anchor tag, it echoes twice

Try it by parameter:
<?=form_open(base_url('main/login')); ?>
or
<?=form_open site_url('main/login')); ?>
In order to append the prefix also

You can Use
<?php echo form_open(base_url(main/login)); ?>
You have to use "echo" rather than because it not works in some browsers....

Related

base url is getting calling an inner folder

When I use echo base_url() in some portions of the application, it works fine.
My base_url() is http://localhost:8080/appname/
But when I do this <?php echo base_url(); ?>data/profile/avatar-5.png
The url appears like http://localhost:8080/appname/admin/data/profile/avatar-5.png where admin is a folder. I guess its because from where it is called from. But yet the base_url() should actually give the right url. Any ideas?
In order to use base_url(), you must first have the URL Helper loaded. This can be done either in application/config/autoload.php:
$autoload['helper'] = array('url');
Or, manually:
$this->load->helper('url');
Also don't forget to set base_url in application/config/config.php
$config['base_url'] = "http://localhost:8080/appname/';
Then try
<?php echo base_url('data/profile/avatar-5.png'); ?>
If the base path is not working for you, you can try my solution which I usually use
Create an separate path for logo or image
$config['image_path'] = ' http://localhost:8080/appname/admin/data/profile/';
use that in you view controller as
echo $this->config->item('image_path').$imagename;

Img Src path adding / when using .SVG in Cakephp

For some reason unknown to me when I try to use a .svg file in the image src path cakephp adds an additional / to the file path which shows the image as missing.
example:
<img src="<?php e($html->url('/img/mobile/shark.svg')); ?>"
outputs:
<img src="host/img/mobile/shark.svg/"
It then thinks the file is not there. But when I remove the / in chrome inspect the file appears. Anyone see this issue before?
Update 5/1
On Cake 1.3, and beyond our control at the moment to update. These helpers just break the page =( and after looking at the documentation for 1.3 it looks like it should not.
Create a image tag using cakephp helper,
<?php echo $this->Html->image('example.svg', array('alt' => 'CakePHP')); ?>
Output will be
<img src="/img/example.svg" alt="CakePHP" />
Look HtmlHelper::image(string $path, array $options = array())
You should use the CakePhp HTML helper.
<?php echo $this->Html->image('image.svg', array('alt' => 'image')) ?>

codeigniter in ubuntu always shows "404 not found"

welcome page only shows. If I create new controller and view means it shows '404 Not found' .
Controller - Create.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Create extends CI_Controller {
public function index()
{
$this->load->view('create');
}
}
View - create.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');?>
<!DOCTYPE html>
<html lang="en">
<body>
<h1>Welcome to CodeIgniter!</h1>
</body>
</html>
I have run like this in browser - "http://example.com/Create/". But it shows 404 Not Found. I gave permission to all files also.
Firstly it showing welcome page because default controller is welcome in application\config\routes.php file
$route['default_controller'] = "welcome";
Only default controller can be use without index.php.
so try this for other controllers "http://example.com/index.php/controllerName/functionName"
You can refer to : https://ellislab.com/codeigniter/user-guide/general/urls.html
Do you have these set?
$config['base_url'] = '';
$config['index_page'] = '';
I think it is an upper case issue. Linux is case sensitive.
The fix is that you make sure you capitalize all your controller file names i.e in the controller folder, instead of welcome.php change it to Welcome.php
I hope this helped.
You need to enable mod_rewrite and set the "AllowOverride" option to "All" in your apache2.conf file.
To enable the mod_rewrite you just need to enter to a terminal and type the folllowing command:
a2enmod rewrite (as root user)
To set the AllowOverride All in the apache2.conf file you need to open it and set the AllowOverride option from None to All in the section, like this:
<Directory /var/www/html/>
AllowOverride All
</Directory>
If it doesn't exist then write it.

Image not retrieve from folder

I am upload the image into database and folder. But when I am trying to retrieve it it will not working.
My code is as below:
<img src="<?php echo APPPATH.'modules/employee/image/'.#$userdata[0]->photo;?>" />
Give me a solution.
Try:
<img src="<?php echo base_url().'modules/employee/image/'.#$userdata[0]->photo;?>" />
Constant APPPATH will not give you full path, you must need to use base_url() instead, but for using base_url() you also need to define your base_url in config.php

Prevent direct access of downloaded file in CI

How to prevent direct access to my download file using CodeIgniter.
Supposed I stored all of my file in
application/secretdirectory/file1.jpg
application/secretdirectory/file2.text
application/secretdirectory/file3.zip
usually, I create direct link to access these files.
<a href="application/secretdirectory/file3.zip" > download here </a>
I want to prevent it, I want my link will be
<a href="<? echo site_url() . "download/file3.zip" ?>" > download here </a>
can you help me ???
It's probably a good idea to pass it to a controller like this:
Class File extends Controller{
function download($file){
$data = file_get_contents('path/to/' . $file); // Read the file's contents
$name = $file;
force_download($name, $data);
}
}
Your link would be:
<?php echo anchor('file/download/' . $thefile, 'Download');?>
This way they'd never know which directory it came from.
I'm sure there is a library in CI to assist with this
basically you send headers for the correct MIME type, the content length (filesize()) and then output to the browser to download echo file_get_contents() may work)
If using Apache, you'll also want to deny showing directoy content.
<Files .*>
Order Deny,Allow
Deny From All
</Files>

Resources