Codeigniter echoing [::1] instead of localhost - php

I am using CodeIgniter 3 as a web platform and trying to import semantic-UI CSS into my page. I'm doing so by using CodeIgniter's base_url() method in the href property for the CSS import.
However, semantic.css itself imports some other fonts on my server, which cannot load because of Cross-Origin resource sharing policy. This is the error message chrome gives me:
Font from origin 'http://[::1]' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
This is because base_url() echoes the domain has been [::1] and not localhost as I've typed into the browser.
For some reason, it appears to me that chrome (and also Edge) does not consider [::1] and localhost as the same host, or maybe I'm just being dumb. What I know though is that if I change the path of the main semantic.css file and complex code localhost into it, it works, and it also works if, instead of requesting my page using localhost, I use [::1]
I've done other projects very similar to this and never had this "[::1]" appear. What exactly is causing PHP to echo such a path?

It's because of your base_url is empty.
In config/config.php
$config['base_url'] = 'http://localhost/project_name';
Something more interesting about http://\[::1\]/

You need to edit your $config['base_url'] as follows,
$config['base_url'] = '';
$config['base_url'] = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
$config['base_url'] .= "://" . $_SERVER['HTTP_HOST'];
$config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']), "", $_SERVER['SCRIPT_NAME']);
File location: codeigniter/application/config/config.php
Use above code to get dynamic url.

More accurate and dynamic way
$root = "http://".$_SERVER['HTTP_HOST'];
$root .= dirname($_SERVER['SCRIPT_NAME']);
$config['base_url'] = $root;
Though you can still use port.

In order to use base_url(); you must first have the URL Helper loaded. This can be done either in application/config/autoload.php (on or around line 67): or you can manually using
$this->load->helper('url');
than set the
$config['base_url'] = 'http://localhost/your_site_url';
i think it will help you

This is what you need to alter in config/config.php, it works properly in "localhost" as well as in your "server":
$config['base_url'] = "http://".$_SERVER['SERVER_NAME'];
$config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
if(!defined('DOCUMENT_ROOT')) define('DOCUMENT_ROOT',str_replace('application/config','',substr(__FILE__, 0, strrpos(__FILE__, '/'))));
$config['base_path'] = constant("DOCUMENT_ROOT");
$config['js_url'] = $config['base_url'].'js/';
$config['css_url'] = $config['base_url'].'css/';
$config['image_url'] = $config['base_url'].'img/';
// Host resolution for cross origin requests
if(ENVIRONMENT == 'production') {
$config['host'] = 'www.<domain_name>.com';
} else {
$config['host'] = 'localhost';
}

Related

Host Header Attack on Codeigniter

i got this issue from IT-Sec, i have read and search thouroghly but i still can't find any actual solution to fix this issue. Here it is.
"HTTP Host header can be controlled by an attacker. This can be exploited using web-cache poisoning and by abusing alternative channels. Pentester try to request with modify header host. and the response result showing with the modify host header. affected files:
app/formulir
app/kompensasi
app/panduan-agen
app/produk-dan-layanan
app/tentang
app/tentang-
app/training
The impact of this vulnerability
An attacker can manipulate the Host header as seen by the web application and cause the application to behave in unexpected ways."
This is the header sc :
header
Recommended solution thus far is :
The web application should use the SERVER_NAME instead of the Host header
This app are running on xampp with reverse proxy setting for testing. I already do 3 changes to config.php, but the issue is still there. Here is the code.
if(isset($_SERVER[SERVER_NAME])) {
$config['base_url'] = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 'https' : 'http';
$config['base_url'] = '://'. $_SERVER['SERVER_NAME'];
$config['base_url'] = str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
}
else{
$config['base_url'] = '';
}
and 2 :
$config['base_url'] = 'http://$_SERVER[SERVER_NAME]';
and 3 :
$config['base_url'] = 'https://jktdc.*********.com/app'
What im asking is, how/where/what exactly i have to change/add to fix this issue. Not a bashing. Thanks a lot.
The answer is
$url = ''
$config[base_url] = $url
so it will accept whatever the servername is.

About HTML and attribute href

Good evening guys, I have a problem with the attribute href because I don't know how to access folders, I give an example:
I stay in root/store/products/details.php
I want to go root/index.php
My answer is : How do I arrive to root/index.php ? I has been trying with ../../index but that didn't worked
If root is the base path on your domain, e.g. example.com/root, then if you're at example.com/root/store/products/details.php, you would have a link like below to get to your root. This href will actually work on any page. If you use ../../index.php it will only work on some pages.
Root
This is how I make all of my template links portable.
For files processed Server-Side you would link by using the SERVER_ROOT and for links in an -a href=...' you would use the SITE_ROOT.
define('SERVER_ROOT', dirname( __FILE__ ) . DS);
$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ?
'https://' : 'http://') . $_SERVER['SERVER_NAME'];
if (strlen( $url ) <= 10) $url = null; // For IDE Support
define( 'SITE_ROOT', $url . DS); // The base URL
So to implement the above
Post Score</li>
Use <a href="../../index.php">
Demo Snippet:
var base = 'https://example.com/store/products/details.php'
var relative = '../../index.php'
var target = new URL(relative, base).toString()
console.log(target)
PHP defaults to index.php so you can link to
Link text
By prefixing a URL with /, like "/root/" instead of "root/", you are forcing it to read from the root of your webhost or with a VPS the directory of the website.
If you customised the DirectoryIndex you can do Link

BASE_URL in codeigniter shows different URL

I have received website code from somebody else & it's giving a lot of errors in loading. Every CSS, image or anything is loaded by this code
href="<?php echo asset_url();?>/css/...
Now I checked, asset_helper.php & found out this
function asset_url(){
return BASE_URL.'public'
}
In my config.php, line says $config['base_url'] = '';.
Finally when I tried to echo asset_url();, it gives me https://somerandomwebsite.com/.... I'm not sure from where this is coming from.
Sorry I'm new to CodeIgnitor & tried everything I could to find out but there was no luck. Can anybody help me in this?
Your Helper function might be using constant function. Check your constan.php file.
define(BASE_URL, "http://test.com");
First things first: the manual!
https://codeigniter.com/user_guide/helpers/url_helper.html?highlight=base_url#base_url
ok so they made a new function just to deal with an extra subfolder name in stead of just doing base_url(). 'public' or base_url('public') and have defined a BASE_URL somewhere which is not part of the core of CI. Track that BASE_URL down and kill it if possible. You can and should use base_url().
I use this for base_url (avoids hardcoded url):
$protocol = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ) ? 'https://' : 'http://';
$config['base_url'] = $protocol . $_SERVER['HTTP_HOST'] . str_replace(basename($_SERVER['SCRIPT_NAME']), "", $_SERVER['SCRIPT_NAME']);
I usually autoload the thing in config/autoload.php:
$autoload['helper'] = array('url');

Auto-rewrite URL in php

I'm wondering if there is a pre-existing PHP function that will rewrite a given URL, adding a pre-defined application context. For example: I have a link like this:
Home
I would want this link to point to "/" under the production environment, but under the development environment it should point to "/app_context/". Is there any PHP function that can do this for me?
You can always change the server config on your production server to match the live server by changing the document root for the production volume (unless you're on a shared host like Bluehost or something like that). There are exceptions to this... example:
BluehostDomain1.com root is /
If you host a second domain with bluehost, then it's a subdirectory (and accessible) of and from the first domain.
BluehostDomain2.com root is / by default and also accessible at /BluehostDomain1.com/BluehostDomain2.com.
In these cases I do something like this:
Make the code look for the domain executing it:
<?php
switch ($_SERVER['SERVER_NAME']){
case "production.server.com":
$dR = '/';
break;
default:
//Dev server dev.server.com
$dR = '/app_context/';
}
$code = 'Home';
$code .= 'About'; // /about/
echo $code;
?>
I usually define a WEBROOT constant that holds this value, and then prepend it to all links:
define('WEBROOT', str_replace('//', '/', dirname($_SERVER['SCRIPT_NAME']) . '/'));
Home
About
Problem solved. I realized that I can just use relative path. For instance:
Home
you can use something like:
<?php
if(/* test for developement environement */) {
echo 'Home';
}
else {
echo 'Home';
}
?>

Make WordPress Available from any domain

I'm trying to create a plugin that will allow WordPress to be accessed from any domain, of course provided that the domain is pointed to it.
I have filter hooks for option_siteurl and option_home which is proving to be useful in almost all cases.
However, it doesn't appear to be working for images that are attached to a post nor for header images of themes. It looks like for these, it's taking the database value of options -> siteurl.
I've tried update_option, but that hasn't done the trick either.
I'm using the following code to get the host:
public function getGoodURL() {
$scheme = ($_SERVER["SERVER_PORT"] == 80 ? "http://" : "https://");
$host = $_SERVER["HTTP_HOST"];
return $scheme.$host;
}
Thanks!
Might want to try putting the site url configuration in the config file i.e.:
$domain = sprintf('%s://%s',
$_SERVER['SERVER_PORT'] == 80 ? 'http' : 'https',
$_SERVER['SERVER_NAME']);
define('WP_SITEURL', $domain);
define('WP_HOME', $domain);
That way, your site will always accept the current domain.

Categories