• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Archives

codeigniter

CodeIgniter Base URL

September 19, 2019

I have a CodeIgniter dev site in Google Cloud. The instance obtains a new IP address at every start. You can set the base URL to a domain or IP address in the “/application/config/config.php” file. Since it’s only a dev box, I don’t have a domain assigned. It’s accessible via IP address only which changes every time the instance is started. So here’s the workaround. I added some extra code in the config file to obtain the external IP address.

$realIP = file_get_contents("http://ipecho.net/plain");
$config['base_url'] = 'http://'.$realIP;

$realIP = file_get_contents("http://ipecho.net/plain"); $config['base_url'] = 'http://'.$realIP;

The code is using a service from ipecho.net which returns your external IP address. Works like a charm!

Filed Under: Cloud Tagged With: base url, codeigniter, dev, dynamic, gcp

HTTPS in CodeIgniter

July 17, 2019

If you switched to HTTPS in Apache, make sure to update CodeIgniter’s config file.

vim /var/www/applications/config/config.php

vim /var/www/applications/config/config.php

Change the base URL to https.

$config['base_url']	= 'https://yourdomain.com/ci/';

$config['base_url'] = 'https://yourdomain.com/ci/';

Filed Under: PHP Tagged With: apache, base url, codeigniter, config, https, php

CodeIgniter 2.1.3 on PHP7

March 18, 2019

If you’re still running older versions of CodeIgniter, here are a couple of tips to make it work with PHP7.

  1. Make sure to install all PHP tools and libraries that comes with PHP7. I was missing curl.
  2. Change your database driver from mysql to mysqli (application/config/config.php).
  3. Fix the /system/core/Common.php error or as some prefer, upgrade to a newer version.

Considering v2.1.3 is very old (currently 3.1.10) , I’m surprised it works with PHP7 with just a few modifications.

Filed Under: PHP Tagged With: codeigniter, curl, mysqli, php7

Modifying the content of your blog

March 14, 2014

I wrote a custom blog in CodeIgniter two years ago. I revisited the code to change the content format a bit. I’ve noticed that there were several pieces of code that were commented out. At the time, I was playing around with what I could potentially do with the content. I thought I would share it here, because it will show you how you could drastically change the content of a blog by manipulating the content.

Let’s say the content of your blog is assigned to a variable called $item->content. We can alter the content by passing PHP functions to it. Let’s say we want to limit the content to just the first 330 characters. We can use a PHP function called substr. Substr returns a portion of the string from a specified start and length.

The Excerpt

$item->content = substr($item->content,0,330);
echo $item->content;

$item->content = substr($item->content,0,330); echo $item->content;

In this example, we are limiting content to the first 330 characters.

Remove page breaks and new lines

$item->content = str_replace(array('\r', '\n'), '', $item->content);
echo $item->content;

$item->content = str_replace(array('\r', '\n'), '', $item->content); echo $item->content;

In this example, str_replace replace page breaks and new lines with ” or nothing.

Remove two spaces

$item->content = str_replace('  ', '', $item->content);
echo $item->content;

$item->content = str_replace(' ', '', $item->content); echo $item->content;

Finally, adding Read More […]

$item->content = $item->content . '<a href=#>Read [...]</a>';
echo $item->content;

$item->content = $item->content . '<a href=#>Read [...]</a>'; echo $item->content;

I appended to content a link with the anchor of ‘Read […].’

Filed Under: PHP Tagged With: blog, codeigniter

URL Spaces in CodeIgniter

February 19, 2014

I ran into some issues with a piece of code I wrote several years ago in CodeIgniter. I’m using names of people as part of the URL. When someone enters a name with spaces on the database, the spaces are converted to %20 in the resulting URL. The URL doesn’t look pretty and definitely not SEO friendly. It needs to be cleaned up by replacing spaces with either a dash or an underscore.

The other issue is when performing a search, the database doesn’t really know that ‘John Doe’ is essentially the same as ‘John%20Doe.’ So, I ended up using a CodeIgniter function called url_title. The function strips the spaces and replaces it with a dash or a hyphen. In the case of ‘John%20Doe’, it now displays as ‘John-Doe.’ You can also use an underscore. See example below.

Replace %20 with dash or hypen

$name = 'John Doe';
$url = url_title($name);
// produces 'John-Doe'
 
$name = 'John Doe';
$url = url_title($name, '_');
// produces 'John_Doe'

$name = 'John Doe'; $url = url_title($name); // produces 'John-Doe' $name = 'John Doe'; $url = url_title($name, '_'); // produces 'John_Doe'

Put the space before the search

Before performing a search, I use str_replace to remove the dash and put back the space so it matches the database entry. It’s essentially the reverse of what I did to display a friendly URL.

$url = 'John-Doe';
$name = str_replace('-', ' ', $url);
// perform your search function here

$url = 'John-Doe'; $name = str_replace('-', ' ', $url); // perform your search function here

If you’re not familiar with CodeIgniter, check how URI routing and url_title are handled.

Filed Under: PHP Tagged With: codeigniter, str_replace, url, url_title

CodeIgniter Anchor With Onclick

January 31, 2013

CodeIgniter comes with a URL helper that assist you with managing URLs. One function worth examining is called anchor, which creates standard HTML links. Instead of the standard HTML <a href=””>anchor</a> tag most people are familiar with, CodeIgniter coders can take advantage of the simpler anchor function.

To create a standard link, all you have to do is code this:

<?=anchor('blog /index', 'Home');?>
 
// Output is
<a href="http://yourdomain.com/blog/index.php">Home</a>

<?=anchor('blog /index', 'Home');?> // Output is <a href="http://yourdomain.com/blog/index.php">Home</a>

If you want to add a Javascript popup that will ask you “Are you sure?” before proceeding to the link, then all you have to do is add a third option in the anchor function like this:

<?php $onclick = array('onclick'=>"return confirm('Are you sure?')");?>
<?=anchor('blog /index', 'Home', $onclick);?>
 
// Output is
<a href="http://yourdomain.com/blog/index.php" onclick="return confirm('Are you sure?')">Home</a>

<?php $onclick = array('onclick'=>"return confirm('Are you sure?')");?> <?=anchor('blog /index', 'Home', $onclick);?> // Output is <a href="http://yourdomain.com/blog/index.php" onclick="return confirm('Are you sure?')">Home</a>

This function works great especially if you add it to code that delete records. You like to be able to ask the user first if that’s what they really want to do before proceeding.

Filed Under: PHP Tagged With: anchor, codeigniter, javascript, url helper

Web Directory Structure

January 16, 2013

If you’re a system administrator or web developer with root privileges, chances are, you probably have complete control of your web server, including root privileges. That’s a good thing. You can setup the server anyway you want. If you’re starting out to build out a new web server, some of the questions that you may have is, what’s the proper or correct way of creating a web directory structure on your web server.

The answer is, there is really no correct way. There’s no magic bullet configuration. You do have complete freedom to setup your server anyway you want. But, if you want to make it easy for yourself, you should probably look for a well-established standard, or some proven way in structuring web directories.

The following is my “suggestion” after my extensive experience with countless web servers over the years. I called it “suggestion” because it’s by no means the holy grail, but the concept has served me well over the years. You may not necessarily agree with my format, or you may have something similar, or something completely different than mine. Nevertheless, my setup is simple and it works.

Most web servers nowadays can host multiple domains. I will make an assumption that you will be running multiple domains on your web server. A typical structure would be something similar to the one below. Each domain has its own subdirectory.

/var/www/domain1.com/
/var/www/domain2.com/
/var/www/domain3.com/

If you have a static website, the file structure would be something like this.

/var/wwww/domain.com/index.html

If you have a dynamic website, you have something similar to this.

/var/www/domain.com/index.php
/var/www/domain.com/wp-content/

By the way, “wp-content” is a WordPress folder where templates and plugins reside. The above structure is serviceable, but if you ever find yourself working with PHP Frameworks such as CodeIgniter, Zend Frameworks, or even Ruby on Rails, there are some additional directories and programs that you would want to hide from the public for security reasons. Here are some directories using the CodeIgniter example.

/var/www/domain.com/index.php
/var/www/domain.com/application/
/var/www/domain.com/system/

To make your application secure, we need to move the “application” and the “system” folders above the web root folder. With the current setup, this is where you begin to have some issues. Where exactly do we put your “application” and “system” folders if multiple domains need them. So, here’s my suggestion. We need to create a “www” folder under each domain. We will make the “www” folder the web root folder. The setup would look something like this.

/var/www/domain1.com/www/
/var/www/domain2.com/www/

Now, that we have the “www” folder created, we can then place the “application” and “system” folders on their own directory above the web root. The setup would look something like this.

/var/www/domain1.com/www/index.php
/var/www/domain1.com/application/
/var/www/domain1.com/system/
/var/www/domain2.com/www/index.php
/var/www/domain2.com/application/
/var/www/domain2.com/system/

So, the setup above is ideal for placing folders above the web root folder if you want a more secure application. At the same time, these folders are still under each domain. So, this is the web structure that I have implemented over the years. If there’s a better way of doing this, I would like to hear about it.

Filed Under: CSS, HTML, PHP Tagged With: codeigniter, structure, web directory, web folders

First Page Is Always Selected

January 4, 2013

I had a little trouble with CodeIgniter pagination today. The pagination function is working fine, but Page 1 is always selected regardless which page I select. The data seems to be correct as I go from one page to another, but the page number doesn’t move at all. Well, after trial and error, the problem turned out to be a configuration issue.

CodeIgniter typically uses the following URI format:

http://domain.com/class/function/id.

The third segment is normally used by the pagination function for navigating from one page to another. By default, CodeIgniter pagination only uses 3 URI segments. If you are using more than 3 segments, then you will need to specify the location of the URI segment in your configuration. In my case, I’m using the fourth segment, I needed to include this setting.

$config [‘uri_segment’] = ‘4’;

If you need help with pagination, please refer to theĀ Pagination class from the CodeIgniter User Guide.

Filed Under: PHP Tagged With: codeigniter, pagination

  • Home
  • About
  • Archives

Copyright © 2023