Use hyphen in CodeIgniter URL

I normally like to use hyphens than underscores in URL. It looks more easier to read I think. But in CodeIgniter normally we can’t use hyphens in URL as hyphens isn’t allowed in function name or class name.

To solve this in CodeIgniter, so the correct function name is found from the uri segment, only one simple change needs to be made.

There are two easier way. First one is:
In system/libraries/Router.php find line 153 and change this line

$segments = $this->_validate_request($segments);

to

$segments = $this->_validate_request(str_replace(“-”, “_”, $segments));

Some people don’t want to edit core codes. In that case, we can use an alternative way.

A better solution is to create a MY_Router.php file in your /application/core directory. In that file you can have the following code:

This is a nicer solution, as it doesn’t alter any segments beyond the controller and method, which is generally what people are after.

post

Removing index.php in codeigniter

Hey Folks, most of the new CodeIgniter developers face a problem to remove index.php from the URL. It’s pretty simple.

Just write the following code in notepad and save as .htaccess. Put this file in the root folder where application, system folders are.

.htaccess code:

or in MAMP

And of course, make sure mod_rewrite is enabled in your server.

Cheers

post

Create a dynamic image gallery using ajax, jquery and php (XML also)

This is a tutorial to create a dynamic image gallery using jQuery ajax and php mainly. To reduce burden, I added a normal script to read directory. In gallery.php file it will read the directory and will generate a xml file which will be thrown directly to the index file (actually ajax in index file will request to execute this file and read the generated the XML formatted data). A lightbox plugin is also used in this script. Let’s have a look:

HTML:

Portfolio

”” ””

CSS:

{code type=css}

*{margin:0; padding:0;outline:none}
.clr{clear:both;}
a{text-decoration:none;}
@font-face{src:url(‘../fonts/Agency_FB.ttf’);font-family:agency;}
body{font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#000000}
ul{list-style-type:none;}
h1, h2, h3, h4{font-family:agency;}
h2{font-size:20px; border-bottom:5px solid #CCCCCC;}
#wrapper{margin:0 auto;width:500px;}
#lightbox{padding:10px;background:url(../images/portfolio.png) repeat;margin-top:10px;border-radius:15px;border:3px solid #CCCCCC;overflow:hidden}
ul.gallery li{float:left; border:1px solid #666666; margin:5px 7px;}
ul.gallery li a{padding:1px; overflow:hidden; display:block}
ul.gallery li a img{width:100px; height:100px; opacity:0.2}

{/code}

JS Code and stylesheet link which should be placed into head section:

{code type=html}

// <![CDATA[
$(document).ready(function() {

$.ajax({
type: “GET”,
url: “gallery.php”,
dataType: “xml”,
success: function(xml){

//pagination for gallery photos
var startIndex = 0; // gets edited via ui
var howMany = 4; // constant of how many per page, but you could make this a ui changeable thing too
var issues = $(xml).find(‘item’); //the resulting issues from the xml
var totalIssues = issues.length;
var displayIssues = function() { // display the issues
var issuesPaginated = issues.slice( startIndex , (startIndex + howMany) );
$(‘ul.gallery’).html(”); // clear old issues
issuesPaginated.each(function(){
var url = $(this).find(‘link’).text();
var alt = $(this).find(‘alt’).text();
$(‘

  • ‘).html(‘<a href=”images/fullscreen/’+url+'” rel=”prettyPhoto
    [gallery1]
    ”>’).appendTo(‘ul.gallery’);
    });
    //lightbox code for gallery after parsing xml
    $(“area[rel^=’prettyPhoto’]”).prettyPhoto();
    $(“.gallery:first a[rel^=’prettyPhoto’]”).prettyPhoto({animation_speed:’slow’,theme:’light_square’,slideshow:3000, autoplay_slideshow: false});
    $(“.gallery:gt(0) a[rel^=’prettyPhoto’]”).prettyPhoto({animation_speed:’slow’,slideshow:10000, hideflash: true});
    $(“#custom_content a[rel^=’prettyPhoto’]:first”).prettyPhoto({
    custom_markup: ‘

    ‘,
    changepicturecallback: function(){ initialize(); }
    });
    $(“#custom_content a[rel^=’prettyPhoto’]:last”).prettyPhoto({
    custom_markup: ‘

    ‘,
    changepicturecallback: function(){ _bsap.exec(); }
    });
    $(‘.gallery a img’).hover(function() {
    $(this).stop().animate({opacity:1}, 1000);
    }, function() {
    $(this).stop().animate({opacity:0.2}, 1000);
    });
    }
    $(‘#prevButton’).click(function() {
    startIndex -= howMany;
    if( startIndex >= 0) {
    //startIndex += howMany;
    displayIssues();
    if((startIndex) <= 0) $(this).hide();

    if(totalIssues – startIndex > howMany) $(‘#nextButton’).show();
    }
    });
    $(‘#nextButton’).click(function() {
    startIndex += howMany;
    if( startIndex < totalIssues) {                         //startIndex += howMany;

    displayIssues();

    if((startIndex + howMany) >= totalIssues) $(this).hide();
    if(totalIssues – startIndex > howMany) $(‘#prevButton’).show();
    }
    });
    displayIssues(); // display for the first time (ajax call);
    }
    });
    });
    // ]]>

    {/code}
    Its not fully tested. Perfectly tested in FF.

    Live in action

    Download

    Thanks.

    post

    PHP: Convert number into Text (Amount into words)

    This is a php function which converts number into text. This function is developed in respect of Bangladeshi Currency (Bangladeshi Taka), but if you know a little php, you can convert it into your currency. Just call the function as

    [php]

    [/php]

    You will get the result.

    Code:
    [php]
    <?php
    function convert_number($number)
    {
    if (($number 999999999))
    {
    throw new Exception(“Number is out of range”);
    }

    $Gn = floor($number / 100000); /* Millions (giga) */
    $number -= $Gn * 100000;
    $kn = floor($number / 1000); /* Thousands (kilo) */
    $number -= $kn * 1000;
    $Hn = floor($number / 100); /* Hundreds (hecto) */
    $number -= $Hn * 100;
    $Dn = floor($number / 10); /* Tens (deca) */
    $n = $number % 10; /* Ones */

    $res = “”;

    if ($Gn)
    {
    $res .= convert_number($Gn) . ” Lacs”;
    }

    if ($kn)
    {
    $res .= (empty($res) ? “” : ” “) .
    convert_number($kn) . ” Thousand”;
    }

    if ($Hn)
    {
    $res .= (empty($res) ? “” : ” “) .
    convert_number($Hn) . ” Hundred”;
    }

    $ones = array(“”, “One”, “Two”, “Three”, “Four”, “Five”, “Six”,
    “Seven”, “Eight”, “Nine”, “Ten”, “Eleven”, “Twelve”, “Thirteen”,
    “Fourteen”, “Fifteen”, “Sixteen”, “Seventeen”, “Eightteen”,
    “Nineteen”);
    $tens = array(“”, “”, “Twenty”, “Thirty”, “Fourty”, “Fifty”, “Sixty”,
    “Seventy”, “Eigthy”, “Ninety”);

    if ($Dn || $n)
    {
    if (!empty($res))
    {
    $res .= ” and “;
    }

    if ($Dn
    [/php]

    post

    PHP: Find maximum odd number of sequence in an array

    This is php program that finds the maximum odd number of sequence in an array. By editing, in case of even number also be found out.

    Code:
    [php]
    <?php
    $x = array(1,7,4,5,7,5,6,3,4,6,87,53,45,567,45,43,66,56,57,5);
    $tot = count($x);
    $y = $res = array();
    $y[0] = array();
    $k = $j = 0;
    for($i = 0; $i $val)
    if($max == count($val))
    $k = $key;

    echo “The number of maximum odd sequesnce: $max
    The numbers are:”;
    var_dump($y[$k]);

    ?>
    [/php]

    Waiting for any better solutions. :)

    post

    A little trick to store data into mysql database from a large form

    Sometimes we feel boring to write mysql insertion query to store data into database from a large form where are too many input fields (such as about 50 fields). We face problem to handle all the fields, sometimes we miss the field name. Here is a small trick to store data from such a large form.

    In this trick, we have to do same naming of table fields and input fields. For example –

    [html][/html]

    Then the field name of the database will be “first_name”

    Then the php code will be:

    Suppose, name of the submit button is submit [html][/html]

    [php]
    safe_entry($_POST); // here obj is the object; safe_entry is a custom function which confirms the input validation of XSS and mysql injection; otherwise you can use $data = safe_entry($_POST); or simply use $data = $_POST;

    $field = $value = array(); //two blank array in which field names and field values will be stored

    foreach($data as $key=>$val)

    if($key != ‘submit’)

    {

    array_push($field, $key);

    array_push($value, $val);

    }

    $fields = implode(‘,’, $field);

    $values = implode(“‘, ‘”, $value);

    $sql = “insert into tablename($fields) values(‘$values’)”;

    $query = $obj->insertQuery($sql); //use direct function mysql_query() in stead of custom function and object

    ?>
    [/php]

    That’s it! This code will store data from a large form. Thanks.

    post

    Setup a web server in Linux Ubuntu

    Try the following code in terminal:

    That’s It! In the installation you may be asked for mysql password for root. Set as you wish but remember it as you will need it.

    Thanks: Ronju vai

    post

    Some Security Issue (php)

    Never, Ever, Trust Your Users

    It can never be said enough times, you should never, ever, ever trust your users to send you the data you expect. I have heard many people respond to that with something like “Oh, nobody malicious would be interested in my site”. Leaving aside that that could not be more wrong, it is not always a malicious user who can exploit a security hole – problems can just as easily arise because of a user unintentionally doing something wrong.

    So the cardinal rule of all web development, and I can’t stress it enough, is: Never, Ever, Trust Your Users. Assume every single piece of data your site collects from a user contains malicious code. Always. That includes data you think you have checked with client-side validation, for example using JavaScript. If you can manage that, you’ll be off to a good start. If PHP security is important to you, this single point is the most important to learn. Personally, I have a “PHP Security” sheet next to my desk with major points on, and this is in large bold text, right at the top.

    Global Variables

    In many languages you must explicitly create a variable in order to use it. In PHP, there is an option, “register_globals”, that you can set in php.ini that allows you to use global variables, ones you do not need to explicitly create.

    Consider the following code:
    [php]
    if ($password == “my_password”) {

    $authorized = 1;

    }

    if ($authorized == 1) {

    echo “Lots of important stuff.”;

    }
    [/php]

    To many that may look fine, and in fact this exact type of code is in use all over the web. However, if a server has “register_globals” set to on, then simply adding “?authorized=1″ to the URL will give anyone free access to exactly what you do not want everyone to see. This is one of the most common PHP security problems.

    Fortunately, this has a couple of possible simple solutions. The first, and perhaps the best, is to set “register_globals” to off. The second is to ensure that you only use variables that you have explicitly set yourself. In the above example, that would mean adding “$authorized = 0;” at the beginning of the script:

    [php]
    $authorized = 0;

    if ($password == “my_password”) {

    $authorized = 1;

    }

    if ($authorized == 1) {

    echo “Lots of important stuff.”;

    }
    [/php]

    Source: www.addedbytes.com/writing-secure-php/writing-secure-php-1/

    Comments are appriciated.

    post

    header() function in the middle of a php page

    We can’t use header() function in the middle of a page in php. header() function is used for redirection. If the page prints something before this function then we get a message like “header already sent by….blablabla”.

    But we have another way to use header() function in anywhere of a page. We have to use two more function to make it working perfectly. Those two are: ob_start() in the first line of a page a and ob_flush() in the last line of that page We can omit the second one but there are some problem. I will discuss it later.

    So, the final syntax is:

    [php]

    [/php]

    This is one type of buffering. The server will buffer all the content and then print together. If there is any header() function then it works first. But of course keep it in mind that, this method will increase program run-time. So, enjoy.

    Any comments are appreciated…