...

Hi! I’m Starx

experienced Software Developer. And this is my blog
Start Reading About me
Blog Page
April 21, 2013

Proper Form Input Sanitation

Undermine2k’s Questions:

I have form fields i’m gathering from my form using

 var dataString = $("form").serialize();

I am then sending this over to my controller as dataType “html”

The var_dump for my dataString looks like this (so far, but it will contain email address, select options, etc)

array(3) {
  ["username"]=>
  string(5) "mikey"
  ["firstname"]=>
  string(4) "tes%"
  ["lastname"]=>
  string(6) "tester" }

my question is as follows: What is the proper method of form sanitation i should be using before I send data to my model? I know I need to strip special characters and the like, is there some prepackaged class I should be using?

Do I need to break my data up like

  $username =  trim(Array[0]) ; 

Enable XSS Filtering on application/config/config.php

$config['global_xss_filtering'] = TRUE;
Read more

Retrieving "Active" state CSS with jQuery

Majed’s Questions:

Is it possible to retrieve the :active state CSS with jQuery? The reason why I ask this is because I’m trying to make dynamic code so I don’t have to always tweak the jQuery when stylizing an element.

Edit

To elaborate, I don’t want to .addClass() or .removeClass() because the class might not be the same for every element being affected by my jQuery code.

Edit 2

Let me further explain what I’m trying to do.

I’m trying to create a plugin for my own personal use, and instead of having to tweak the code every time I have a new element that will be affected by the plugin, I want it to grab what’s already in the CSS so I won’t have to lose time. What I’m trying to do is create a button with an :active state, and when the user clicks the button, it will “freeze” at that state (my thoughts are to grab the CSS from that state and put them in the .css() command). Now, the reason why I don’t want to .addClass() or removeClass() because the :active state is going to differ from one button to another.

Pseudo classes such as :active cannot be retrieved and manipulated from jQuery. Instead of trying to get this work, I have a workaround to solve this problem.

First create a style container with only the :active part. For example:

<style id="activeLink">
    a:active { color: #f00; }
</style>

Now you can manipulate this using the jQuery, to retrieve current styles

var curStyle = $("#activeLink").html();

To modify the style

$("#activeLink").html("a:active { color: #000; }");
Read more

Forcing line on navigation bar (unordered list)?

Rachelle Bennington’s Questions:

My navigation bar currently is scrunching all my text together. I have “headers” for the dropdown list, and the headers aren’t forcing a line.

The HTML looks like this:

<li><p>Services</p><ul>
    <li id="ITServices"><p>IT Services</p></li>
    <li><a href="port_collab_work.html">Portals, Collaboration & Workflows</a></li>
    <li><a href="business_intel_dash.html">Business Intelligence & Dashboards</a></li>
    <li><a href="mobile_development.html">Mobile Development</a></li>
    <li><a href="custom_application_development.html">Custom Application Development</a></li>
    <li id="healthcare"><p>Healthcare Services</p></li>
    <li><a href="healthcare.html">EMR, ICD 10 and Healthcare Consulting</a></li>
</ul></li>

CSS looks like this:

#healthcare p {
    width: 280px;
    margin-left: 0px;
    padding: 0px;
    display: inline;
}

#ITServices p {
    width: 280px;
    margin-left: 0px;
    padding: 0px;
    display: inline;
}

.navbar li:hover ul {
    left: 15px;
    top: 40px;
    background: #7FBA00;
    padding: 1px;
    width: 280px;
    border: none;
    text-align: left;
}

.navbar li:hover ul a {
    margin: -7px -10px -7px -15px;
    text-align: left;
    padding: 0px 0px 0px 10px;
    display: block;
    font-size: 11px;
    width: 259px;
    line-height: 25px;
    color: #000;
    background-color: #F0F0F0;
    text-decoration: none;
    border-left: 10px solid #7FBA00;
    border-bottom: 1px solid transparent;
    border-right: 1px solid transparent;
    border-top: 1px solid transparent;
}

.navbar li:hover ul a:hover {
    background: #7FBA00;
    border-left: solid 10px #fff;
    border-top: solid 1px #fff;
    border-bottom: solid 1px #fff;
    width: 260px;
}

Ahhh! Right? I’m trying to get it to all display in a list with basically line breaks after each li element. Help?

Basically a rule is over-riding your style. display property called block makes an element to behave like a block element, thus covering full line.

Your use might be the following, so try this

li > ul li { display: block; }
Read more
April 9, 2013

Append without last element

Joesandeek’s Questions:

<div id="container">
    <div class="sub">a</div>

    <span id="add">add</span>
</div>            

$('#add').click(function(){
   $('#container').append('<div class="sub">a</div>');
})

This append element to #container on bottom. How can i add this element on bottom without last element(#add)? I would like have always #add on bottom.

Fiddle: http://jsfiddle.net/nk67d/

Try

$("#add").prepend('<div class="sub">a</div>');

See fiddle: http://jsfiddle.net/nk67d/1/

Use .before(), it adds the markup before the matched elements.

$('#add').before('<div class="sub">a</div>');

To complete the answer:

$('#add').click(function(){
   $(this).before('<div class="sub">a</div>');
});
Read more

How to apply WHERE clause again array or jscon encoded value

M4l33n’s Questions:

Values are stored under company_id like

["2"]
["2", "1"]

where 2 and 1 are the IDs of companies. Now i want all result of ID 2. How can i fire query again json encoded data.

select * from tbl where company_id = 2

This is what I need to perform.

For more info, json format is the result of dynamic (fill by database values) Select List in Zend2.

And what if it was array instead of json, how this simple select query can be executed.

As each ID is a number inside double quotes you can just query with a LIKE statement:

select * from tbl where company_id LIKE '%"2"%'

And what if it was array instead of json, how this simple select query can be executed.

You can store JSON into a mysql database because it is a string. But an array is not a string, so you can not store it there. You would need to serialize it but for that you should prefer JSON.

The alternative would be to create a table that stores the relationships to the companies.

MySQL has a datatype called SET You can use this data type on this task. This will allow you to enter comma separated values and still be able to query as per single item.

Manual: http://dev.mysql.com/doc/refman/5.0/en/set.html

Your query will have to be updated to something like this:

SELECT * FROM tbl_name WHERE FIND_IN_SET('2', company_id);

But, this problems arises because the tables are not normalized properly. Adding a reference table and querying this reference table will be a much better option, when the application reaches large-scale.

Read more
April 7, 2013

Append to file…will I need to lock

User1684072’s Questions:

Php newbie here

I am creating a webpage that stores some information in a text file by appending to it.
Everytime the webpage loads, there is a small php script that adds information to the end of the text file. I am using file_put_contents. Here is VERY simplified version of my code:

<?php
$file = "records.txt";
$current = file_get_contents($file);
$current .= "id = ". $_GET["id"]." n";
file_put_contents($file, $current );
?>

Here is my concern…if hundreds of people open my webpage, will my script be able to capture ALL the user information without missing anyone. This is extremely important.

I am afraid to lock it(use LOCK_EX) because that would mean that when a new user opens up the webpage the script would not be able to open up and append to the text file if another user is writing to it and thus I would not be able to capture his information which is a BIG problem.

So should I ignore lock or is one needed?? How should I solve this problem

Thanks a lot.

Use fopen() with a switch. This will handle all the problems.

$handle = fopen("somefile.txt", "a");

For your requirements you should not lock the file, but this will probably expose the file to vulnerabilities. So I will suggest an alternative instead.

Instead of a file, insert the information you want on the database.

Read more
April 4, 2013

$.ajax doesn't response

Nurhidayat Fembrianto’s Questions:

i have some code here that doesn’t work at all:

    <script type="text/javascript">
    function get_all(){
        var mutation = document.getElementById("mutation");
        $.ajax({
            url:"http://localhost/mandala/test/xx",
            success: function(msg){
                mutation.innerHTML = msg;
            },
            error: function(x,status,error){
                alert(status+":"+error);
            }
        });

    }
</script>

<html>
    <body>
        <input type="button" onclick="get_all()" value="Click">
        <div id="mutation">

        </div>
    </body>
</html>

I don’t think so if there is any problem with my url neither the code. But i hope some body can help me out with this problem.

Your HTML Structure in invalid. At the time of Script execution, it will not find the element. So use this instead.

function get_all(){
    $.ajax({
        url:"http://localhost/mandala/test/xx",
        success: function(msg){
            $("#mutation").html(msg); //<!-- jQuery Selector
        },
        error: function(x,status,error){
            alert(status+":"+error);
        }
    });

}
Read more
April 3, 2013

how can i obtain an entire element from an external page?

Cola89’s Questions:

how can i get a

<table class="precios"> 

from another url (www.example.com) in HTML format?? because with DOM i can obtain the table but in an array mode.

Thank you everyone for helping

Assuming you have no cross-domain issues, you can use .load() for that:

$container.load('http://www.example.com/path/to/page table.precios');

Whereby $container is a jQuery object where you want to “save” the table into.

Within PHP you would solve it this way:

$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTMLFile('http://www.example.com/path/to/page.html');
libxml_clear_errors();
$xp = new DOMXPath($doc);
$table = $xp->query('//table[@class="precios"]')->item(0);

echo $doc->saveHTML($table);

Make a AJAX request to that page and use .find() to get the element

$.ajax( {
  url: myUrl,
  success: function(html) {
    table = $(html).find(".precious");
    }
});
Read more

PHP files repetitively and automatically being deleted from server folder

Karancan’s Questions:

I work with a group of about 6 to 7 other developers and we are facing a very strange problem. For quite a few weeks now, we’ve had a problem with four or five different PHP files getting deleted on what appears to be random intervals.

All but one of us is using Aptana Studio as our IDE. One person is using NetBeans. We have created PHP projects on Aptana and we sync over SFTP with a server running CentOS. The server in question is our development server. Half of us are running Windows and the other half are on Mac.

There seems to be little to suggest that it is one of our machines / IDE’s causing this issue. To test this, we have had days where we would leave out one person or a combination of people and see if the problem goes away but unfortunately we have been unable to find any pattern.

The one thing that is fairly consistent though is that the problem tends to occur between after noon and 5 pm. It started with a file called config.php being the only one affected. Then it spread to files called header.php and footer.php. At times it happens that one or more of these files get deleted almost every minute. All these files are in the same directory and nothing outside of this directory (called includes) has had this issue.

The permissions for all the directories and their descendants is set to 775 i.e. no write access to anyone who doesn’t have an SFTP account.

Does anyone know anything about Aptana or NetBeans- something to suggest that it is the IDE causing this issue? Or any thing else for that matter that could help us solve this mystery?


Edit 1: We do have GIT set up so we are covered but nevertheless this is a huge annoyance

Edit 2: There is nothing on the cron tab that would have any impact on these files

Edit 3: The pages are on an intranet and we are certain that there is no virus causing this

Edit 4: Everyone uses the same account for the SFTP connection

Edit 5: We got everyone to stop using an IDE and work on files using FileZilla. The problem persists.

Two things come to my mind about this

  • It might be a cron setup to delete files. Check your crons.
  • Or it can be a virus, that searches and deletes authentication related files from the site, like Config.php and etc
Read more
April 2, 2013

Has something changed?

A day begins with someone in my mind and I ask myself
Has something changed?

A moment of silence brings a smiling face infront of me
and I ask myself, Has something changed?

That sweet voice feels as a music full of love sang to me,
and I ask myself, Has something changed?

Few smiles on her face are everything else starts to fade away
and I ask myself, Has something changed?

Tiny bicker deems our whole day as The sad and a year long
and I ask myself, Has something changed?

Even death is seeming to be a better choice over her
and I ask myself, Has something changed?

Getting lost in her thought is the most beautiful magic around
and I ask myself, Has something changed?

Time spent, many attempts struggling just to finish this poem
and I ask myself, Has something changed?

There is a hint of satisfaction, as I am typing these words
So……… I ask. Has something changed?

Yes,
My whole life changed, when a precious little flower became my life
and everything  in it

Read more
...

Please fill the form - I will response as fast as I can!