Fropper.com - no one's a stranger
Already a member? Login here  | Tour | Help  
in


php tips and tacts n some unseen functions yet powerfull.....



Nov 07, '08



Hi , friendz

Next  thursday, November 13,  is the official event for OpenSocial's first birthday being hosted by the OpenSocial Foundation at MySpace's SF office (SOMA).

Highlights of event

* OpenSocial: State of the Union

* App Developers sharing their experiences:
       PhotoBuzz, RockYou, and PixVerse

* OpenSocial 0.9: What does it mean for me?

* Demos of Developer Tools:
     OpenSocial Dev App, zembly, and iWidgets

* "Global Container Crawl" programming contest
        How many containers can you get running on?
        Engineers from around the world will be on-hand to help
           including: hi5, Hyves, iGoogle, LinkedIn, MySpace, Ning, orkut, Viadeo, Yahoo!  among  
           others

* Breakout sessions for discussions and prototyping


Check the full post
http://opensocialapis.blogspot.com/2008/11/opensocials-first-birthday-detailed.html







Nov 07, '08




PHP filters are used to validate and filter data coming from insecure sources.
e.g. User Inputs...

PHP filter_input() Function

With filter_input() function we can filter out the data from following sources.

1. INPUT_GET
2. INPUT_POST
3. INPUT_COOKIE
4. INPUT_ENV
5. INPUT_SERVER
6. INPUT_SESSION (Not yet implemented)
7. INPUT_REQUEST (Not yet implemented)

this function returns the filtered variable data on success and returns FALSE on the  failure or of filter action.

Syntax
filter_input(input_type, variable, filter, options)

input_type =  Above List of sources. e.g . INPUT_GET , INPUT_POST.
variable     =  Variable to be validated.
filter           =  Specifies the ID of the filter to use. Default is FILTER_SANITIZE_STRING.

Here is the list of php filters.
ID Name                                            Description
FILTER_CALLBACK------------------Call a user-defined function to filter data
FILTER_SANITIZE_STRING-------Strip tags, optionally strip or encode special characters
FILTER_SANITIZE_STRIPPED--- Alias of "string" filter
FILTER_SANITIZE_ENCODED--- URL-encode string, optionally strip or encode special
                                                            characters
FILTER_SANITIZE_SPECIAL_CHARS
                                                            HTML-escape '"<>& and characters with ASCII value less than
                                                            32
FILTER_SANITIZE_EMAIL----------Remove all characters, except letters, digits and               
                                                            !#$%&'*+-/=?^_`{|}~@.[]
FILTER_SANITIZE_URL------------Remove all characters, except letters, digits and $-_.+!*'(),
                                                            {}|\\^~[]`<>#%";/?:@&=
FILTER_SANITIZE_NUMBER_INT----Remove all characters, except digits and +-
FILTER_SANITIZE_NUMBER_FLOAT----Remove all characters, except digits, +- and optionally
                                                            .,eE
FILTER_SANITIZE_MAGIC_QUOTES----- Apply addslashes()
FILTER_UNSAFE_RAW----------------------- Do nothing, optionally strip or encode special
-------------------------------------------------------- characters
FILTER_VALIDATE_INT----------------------- Validate value as integer, optionally from the specified
-------------------------------------------------------- range
FILTER_VALIDATE_BOOLEAN-------------- Return TRUE for "1", "true", "on" and "yes", FALSE for
-------------------------------------------------------- "0", "false", "off", "no", and "", NULL otherwise
FILTER_VALIDATE_FLOAT------------------- Validate value as float
FILTER_VALIDATE_REGEXP---------------- Validate value against regexp, a Perl-compatible
------------------------------------------------------- regular expression
FILTER_VALIDATE_URL--------------------- Validate value as URL, optionally with required
------------------------------------------------------  components
FILTER_VALIDATE_EMAIL------------------  Validate value as e-mail
FILTER_VALIDATE_IP------------------------ Validate value as IP address, optionally only IPv4 or
------------------------------------------------------ IPv6 or not from private or reserved ranges


 









Nov 06, '08



What is JSON ?

Javascript Object Notation

JSON make easy to interchange between programming language to JavaScript.

JSON is class/library that we can use to convert a variable from PHP variable to JavaScript Variable.

its another way to use javascript on your site and not have to worry about people having it turned off since doesn't require javascrpt tags to actually run it.


it make programing in javascript quicker and more effective.


It is a Fat-Free Alternative to XML.

It is a new data interchange format.

JSON data can be included from websites outside the domain origin of the web application (avoiding the same-origin limitations of XMLHttpRequest) by using JSON-in-script.


JSON can be be used as an alternative to XML representation of data with the added benefits that JSON is easier to "parse" for JavaScript client code


Sample code snippet to generate JSON string for php array.

<?php
$myArray = Array('a'=>'the first', 'b'=>'is second');

$json = new JSON();

echo $json->convert($myArray);
?>


1.) You can download the JSON.php file from this link:
http://mike.teczno.com/JSON/JSON.phps

check out following code.

require_once("JSON.php");

$myArray = Array('a'=>'the first', 'b'=>'is second');

$json = new Services_JSON();

echo $json->encode($myArray);

Output :
 {"a":"the first","b":"is second"}


How to turn xml into json:

 http://www-128.ibm.com/developerworks/xml/library/x-xml2jsonphp/







Nov 06, '08



What is JSON ?

Javascript Object Notation

JSON make easy to interchange between programming language to JavaScript.

JSON is class/library that we can use to convert a variable from PHP variable to JavaScript Variable.

its another way to use javascript on your site and not have to worry about people having it turned off since doesn't require javascrpt tags to actually run it.


it make programing in javascript quicker and more effective.


It is a Fat-Free Alternative to XML.


JSON data can be included from websites outside the domain origin of the web application (avoiding the same-origin limitations of XMLHttpRequest) by using JSON-in-script.


JSON can be be used as an alternative to XML representation of data with the added benefits that JSON is easier to "parse" for JavaScript client code


Sample code snippet to generate JSON string for php array.

<?php
$myArray = Array('a'=>'the first', 'b'=>'is second');

$json = new JSON();

echo $json->convert($myArray);
?>
testing







Nov 05, '08



How to check the empty directory in shell script?

This can be easily done with the help ls command.
we can simply check the empty directory using following script.

DIR="/path/to/directory/"

if [ "$(ls -A $DIR)" ]; then
           echo "Take action $DIR is not Empty"
else
           echo "$DIR is Empty"

You can also assign the output of specific command to a separate variable instead of checking it directly in if condition.
e.g.

myVar=`(grep 'stringToSearch' filename.ext)`;

Note: Do not use spaces before and after =.







Nov 04, '08



How to detect the firebug.

I use this to detect the firebug.

<script language="javascript">
if(typeof window.loadFirebugConsole != "undefined")
{
  alert("Firebug is enabled..");
}
</script>







Oct 27, '08



CSS Property JavaScript Reference

background -> background
background-attachment -> backgroundAttachment
background-color -> backgroundColor
background-image -> backgroundImage
background-position -> backgroundPosition
background-repeat -> backgroundRepeat
border -> border
border-bottom -> borderBottom
border-bottom-color -> borderBottomColor
border-bottom-style -> borderBottomStyle
border-bottom-width -> borderBottomWidth
border-color -> borderColor
border-left -> borderLeft
border-left-color -> borderLeftColor
border-left-style -> borderLeftStyle
border-left-width -> borderLeftWidth
border-right -> borderRight
border-right-color -> borderRightColor
border-right-style -> borderRightStyle
border-right-width -> borderRightWidth
border-style -> borderStyle
border-top -> borderTop
border-top-color -> borderTopColor
border-top-style -> borderTopStyle
border-top-width -> borderTopWidth
border-width -> borderWidth
clear -> clear
clip -> clip
color -> color
cursor -> cursor
display -> display
filter -> filter
font -> font
font-family -> fontFamily
font-size -> fontSize
font-variant -> fontVariant
font-weight -> fontWeight
height -> height
left -> left
letter-spacing -> letterSpacing
line-height -> lineHeight
list-style -> listStyle
list-style-image -> listStyleImage
list-style-position -> listStylePosition
list-style-type -> listStyleType
margin -> margin
margin-bottom -> marginBottom
margin-left -> marginLeft
margin-right -> marginRight
margin-top -> marginTop
overflow -> overflow
padding -> padding
padding-bottom -> paddingBottom
padding-left -> paddingLeft
padding-right -> paddingRight
padding-top -> paddingTop
page-break-after -> pageBreakAfter
page-break-before -> pageBreakBefore
position -> position
float -> styleFloat
text-align -> textAlign
text-decoration -> textDecoration
text-decoration: blink -> textDecorationBlink
text-decoration: line-through -> textDecorationLineThrough
text-decoration: none -> textDecorationNone
text-decoration: overline -> textDecorationOverline
text-decoration: underline -> textDecorationUnderline
text-indent -> textIndent
text-transform -> textTransform
top -> top
vertical-align -> verticalAlign
visibility -> visibility
width -> width
z-index -> zIndex







Oct 24, '08



Steps to create orkut applications.

Platform

    Orkut applications can be created in XML.

1. Start with the sandbox account on http://sandbox.orkut.com .
     Register on http://code.google.com/support/opensocialsignup/
2. Once your account get active , you can start developing your application in sandbox.
3. You can add your XML application by clicking on add application tab.

HELP
http://code.google.com/apis/orkut/docs/orkutdevguide.html

First Time developing application?

http://code.google.com/apis/orkut/articles/tutorial/tutorial.html


Tags: php, orkut





Oct 10, '08



Fatal error: Allowed memory size of 8388608 bytes exhausted.

sometime PHP script may returns the above error. Normally this error is generated when your script exchausted and used up the default memory requirement of 8 MB memory allocation.

* Default memory limit is set in php.ini file ( php configuration file.)

* memory_limit = 8M;

How can we manage with the memory problems comes in some php scripts?
you should check following things when such kind of memory errors occurs in your script.

1. check the default memory_limit variable in your php.ini file.
    memory_limit = 8M;
    you can change this memory limit and again check for the error.

   a. Edit in php.ini
        memory_limit = 12M;
        restart the apache service.
   b. code Level
        @ini_set('memory_limit', '12M');
  c. htaccess
      php_value memory_limit 12M
___________________________________________________________________

2. If the error is not resolved after above step, then you should start debugging your code in order to find out the reason of memory limit exceed.
    You have to first find out at what position in your code the memory limit is exceeding. you can use the php's built in function for this purpose.

Function description

memory_get_usage — This function returns the amount of memory allocated to PHP.

Syntax : int memory_get_usage ( true / false );

It returns the amount of memory, in bytes, that's currently being allocated to your PHP script.

Set the option to this function to TRUE to get the real size of memory allocated from system. If not set or FALSE only the memory used by emalloc() is returned.

How to use this function

1. Write this code ( echo memory_get_usage( true ); ) repeteadly after some no of lines.
2. Run it in browser
3. Compare the counts given by each echo and you can check which block of your code needs extra memory.
4. Once you identify the block of code which needs extra memory then you can start deallocating the unused memory spaces allocated by some unused variables and some infinite loops.
5. use following wherever necessary.
     a. unset($var_name);
     b. mysql_free_result($result_set); — Free result memory
     c. Look for include("file.php") in loops, by mistake.
     Just try to free the memory everywhere if the variable / array is no longer used.
6. Best of luck.


Function description

bool mysql_free_result ( resource $result )
It frees all the memory associated with the result identifier result .








Oct 08, '08



A shell is a command line interpretor. It takes commands and executes them. We can run N no of common commands one after another by putting them in one file( shell script ) in proper order.

Creating a simple shell file

suppose you want to view the some files in some directory regularly. so you need to use following command:
find /path/to/folder/*.ext  -mtime 30 -type f -exec ls -lh {} \;

This will display all files which are 30 days older from given folder path .

we can do this task in shell script as follows

#!/bin/sh
find /path/to/folder/*.ext -mtime 30 -type f -exec ls -lh {} \;
exit 0

_______________________________________________________________

How to create shell script file with vi editor.

1. vi file_name.sh                                                - create the shell file

2. First statement of shell file should be
    #!/bin/sh                                                            - start of shell script

3. write all commands one after another.      - all commands to be executed

4.End of shell script                                           - end of shell script
   exit 0

5.come out of vi editor and run the shell file  - run the shell file
    ./file_name.sh

simple.................
_______________________________________________________________  

Passing value to shell script
We can also pass the arguments to our shell script. In our example we are displaying all the files which are modified 30 days before. Now suppose we want to see all the files which are  modified 10 days before , then we need to make change in our shell script. To avoid this we can pass the date count to our script from command line. Just go through the sample file given below.

e.g.
#!/bin/sh
clear
echo *SCRIPT NAME = $0
echo *No Of Parameters = $#
echo *Parameters List = $*
echo List of files modified before $1 days.
find /path/to/folder/*.ext -mtime $1 -type f -exec ls -lh {} \;
exit 0

* echo is used to write output.
* $0 gives the script name.
* $# gives no of parameter count.
* $* gives a string of all parameters to script
* Individual argument can be accessed with $1,$2,$3,$4,$5.............
  $1 for first parameter and so on..

How to execute
./file_name.sh +10
In this case +10 is passed to our shell script as first parameter and thus script runs accordingly.