domingo, 28 de febrero de 2016

PHP: Hackers Paradise


PHP: Hackers Paradise




by Nathan Wallace




1         Introduction




PHP (http://www.php.net) is a powerful server side web scripting solution. It has quickly grown in popularity and according to the 1999 January Netcraft Web Server Survey PHP is installed on 12.8% of all web sites.  Much of its syntax is borrowed from C, Java and Perl with a couple of unique PHP-specific features thrown in. The goal of the language is to allow web developers to write dynamically generated pages quickly.



Being a good PHP hacker isn’t just about writing single line solutions to complex problems.  For example, web gurus know that speed of coding is much more important than speed of code.  In this article we’ll look at techniques that can help you become a better PHP hacker.  We’ll assume that you have a basic knowledge of PHP and databases.



If nothing else, you should leave here with the 3 key ideals for PHP hackers:

·       Laziness is a Virtue

·       Chameleon Coding

·       Speed of Coding, Not Speed of Code



2         Laziness is a Virtue


2.1.1     Introduction




It seems strange to think of a web programmer as lazy.  Most of us work one hundred-hour week’s in our quest to join the gold rush.  In fact, we need to be lazy because we are so busy.



There are two key ways to be lazy.  Firstly always use existing code when it is available, just integrate it into your standards and project.  The second technique is to develop a library of helpful functions that let you be lazy in the future.



2.1.2     Use Other People’s Code




We need to use laziness to our advantage and PHP is the perfect tool.  PHP was born and raised in an open source environment.  The community holds open source ideals close to its heart.  As a result there are thousands of people on the mailing list willing to share their knowledge and code.  There are also many open source PHP projects that you can tap into.

I’m not suggesting that you spend all day asking people to write code for you.  But through clever use of the knowledge base, mailing list archives and PHP projects you can save yourself a lot of time.



PHP Knowledge Base – http://php.faqts.com






2.1.3     Helpful Functions and Classes




2.1.3.1      Introduction




In this section we will work at developing a library of PHP code which will aid us in future development.  A small amount of work now let’s us be lazy in the future.



Some of this code has been taken from open source PHP projects.  Other parts from the mailing list archives.  In fact, all the work I really needed to do was structure the code into a coherent library of functions.





2.1.3.2      Database Abstraction




One of the features / problems with PHP is that it does not have a uniform method for accessing databases.  There are specialized functions for each database PHP is able to connect to.  This is a feature because it allows you to optimize your database code.  It is a problem because it makes your code less portable and increases the learning curve for newcomers.



A number of database wrapper classes have been developed to solve this problem.  They provide a uniform set of functions for accessing any database.  Personally I like them because I find it much easier to remember a few simple functions like query and next_record than having to think about database handles, connections and so on.



The most commonly used (and defacto standard) is PHPLib - http://phplib.netuse.de/






There is also PHPDB - http://phpdb.linuxbox.com/




2.1.3.3Session Management




The main purpose of PHPLib is session management.  This allows you to associate data with visitors to your site for the duration of their stay.  This can be useful for remembering options and so on.



PHP4 has session management features built into the PHP internal function library.





2.1.3.4      Debugging Variables




There is limited debugging support for PHP. This is no Smalltalk environment where you can browse objects and perform methods on them. Instead we need to make creative use of the old, reliable echo statement.



The first thing we need to be able to do is look at the value of variables. The loose typing of PHP lets us use most variables directly in strings. This is great for numbers and so on, but falls down when we are dealing with arrays and objects.



The other problem with debugging is that sometimes I'm not even sure what a variable is likely to contain. If I was, there be no need to debug.



So, lets be smart now and lazy for the rest of time. We can write a function that shows us the type and value of any variable.



function ss_array_as_string (&$array, $column = 0) {

    $str = "Array(<BR>\n";

    while(list($var, $val) = each($array)){

        for ($i = 0; $i < $column+1; $i++){

            $str .= "&nbsp;&nbsp;&nbsp;&nbsp;";

        }

        $str .= $var.' ==> ';

        $str .= ss_as_string($val, $column+1)."<BR>\n";

    }

    for ($i = 0; $i < $column; $i++){

        $str .= "&nbsp;&nbsp;&nbsp;&nbsp;";

    }

    return $str.')';

}


function ss_object_as_string (&$object, $column = 0) {

    if (empty($object->classname)) {

        return "$object";

    }

    else {

        $str = $object->classname."(<BR>\n";

        while (list(,$var) = each($object->persistent_slots)) {

            for ($i = 0; $i < $column; $i++){

                $str .= "&nbsp;&nbsp;&nbsp;&nbsp;";

            }

            global $$var;

            $str .= $var.' ==> ';

            $str .= ss_as_string($$var, column+1)."<BR>\n";

        }

        for ($i = 0; $i < $column; $i++){

            $str .= "&nbsp;&nbsp;&nbsp;&nbsp;";

        }

        return $str.')';

    }

}



function ss_as_string (&$thing, $column = 0) {

    if (is_object($thing)) {

        return ss_object_as_string($thing, $column);

    }

    elseif (is_array($thing)) {

        return ss_array_as_string($thing, $column);

    }

    elseif (is_double($thing)) {

        return "Double(".$thing.")";

    }

    elseif (is_long($thing)) {

        return "Long(".$thing.")";

    }

    elseif (is_string($thing)) {

        return "String(".$thing.")";

    }

    else {

        return "Unknown(".$thing.")";

    }

}



Note that these functions work together to correctly print, format and indent arrays. They are also able to print objects when they have been defined with the PHPLIB standard variables classname (the name of the class) and persistent_slots (an array of the variable names we care about).



Now we can see the state of any variable by just doing:



    echo ss_as_string($my_variable);



We can see the value of all variables currently defined in the PHP namespace with:



    echo ss_as_string($GLOBALS);

2.1.3.5      Log Functions




A great way to debug is through logging.  It’s even easier if you can leave the log messages through your code and turn them on and off with a single command.  To facilitate this we will create a number of logging functions.



$ss_log_level = 0;

$ss_log_filename = '/tmp/ss-log';

$ss_log_levels = array(

    NONE  => 0,

    ERROR => 1,

    INFO  => 2,

    DEBUG => 3);



function ss_log_set_level ($level = ERROR) {

    global $ss_log_level;

    $ss_log_level = $level;

}



function ss_log ($level, $message) {

    global $ss_log_level, $ss_log_filename;

    if ($ss_log_levels[$ss_log_level] < $ss_log_levels[$level]) {

        // no logging to be done

        return false;

    }

    $fd = fopen($ss_log_filename, "a+");

    fputs($fd, $level.' - ['.ss_timestamp_pretty().'] - '.$message."\n");

    fclose($fd);

    return true;

}



function ss_log_reset () {

    global $ss_log_filename;

    @unlink($ss_log_filename);

}



There are 4 logging levels available.  Log messages will only be displayed if they are at a level less verbose than that currently set.  So, we can turn on logging with the following command:



ss_log_set_level(INFO);



Now any log messages from the levels ERROR or INFO will be recorded.  DEBUG messages will be ignored.  We can have as many log entries as we like.  They take the form:



ss_log(ERROR, "testing level ERROR");

ss_log(INFO, "testing level INFO");

ss_log(DEBUG, "testing level DEBUG");


This will add the following entries to the log:



ERROR – [Feb 10, 2000 20:58:17] – testing level ERROR

INFO – [Feb 10, 2000 20:58:17] – testing level INFO



You can empty the log at any time with:



ss_log_reset();





2.1.3.6      Optimization




We need a way to test the execution speed of our code before we can easily perform optimizations.  A set of timing functions that utilize microtime() is the easiest method:



function ss_timing_start ($name = ‘default’) {

    global $ss_timing_start_times;

    $ss_timing_start_times[$name] = explode(' ', microtime());

}



function ss_timing_stop ($name = ‘default’) {

    global $ss_timing_stop_times;

    $ss_timing_stop_times[$name] = explode(' ', microtime());

}



function ss_timing_current ($name = ‘default’) {

    global $ss_timing_start_times, $ss_timing_stop_times;

    if (!isset($ss_timing_start_times[$name])) {

        return 0;

    }

    if (!isset($ss_timing_stop_times[$name])) {

        $stop_time = explode(' ', microtime());

    }

    else {

        $stop_time = $ss_timing_stop_times[$name];

    }

    // do the big numbers first so the small ones aren’t lost

    $current = $stop_time[1] – $ss_timing_start_times[$name][1];

    $current += $stop_time[0] – $ss_timing_start_times[$name][0];

    return $current;

}



Now we can check the execution time of any code very easily.  We can even run a number of execution time checks simultaneously because we have established named timers.



See the optimizations section below for the examination of echo versus inline coding for an example of the use of these functions.






2.1.3.7      Debugging and Optimizing Database Operations




The best way to gauge the stress you are placing on the database with your pages is through observation.  We will combine the logging and timing code above to assist us in this process.



We will alter the query() function in PHPLib, adding debugging and optimizing capabilities that we can enable and disable easily.



function query($Query_String, $halt_on_error = 1) {

    $this->connect();

    ss_timing_start();

    $this->Query_ID = @mysql_query($Query_String,$this->Link_ID);

    ss_timing_stop();

    ss_log(INFO, ss_timing_current().’ Secs – ‘.$Query_String);

    $this->Row   = 0;

    $this->Errno = mysql_errno();

    $this->Error = mysql_error();

    if ($halt_on_error && !$this->Query_ID) {

      $this->halt("Invalid SQL: ".$Query_String);

    }

    return $this->Query_ID;

}



3         Chameleon Coding


3.1.1     Introduction




A chameleon is a lizard that is well known for its ability to change skin color.  This is a useful metaphor for web programming as it highlights the importance of separating well structured and stable backend code from the dynamic web pages it supports.



PHP is the perfect language for chameleon coding as it supports both structured classes and simple web scripting.



3.1.2     Structuring your PHP Code




3.1.2.1      Introduction




When writing PHP code we need to make a clear distinction between the code which does the principal work of the application and the code which is used to display that work to the user.



The backend code does the difficult tasks like talking to the database, logging, and performing calculations.



The pages that display the interface to these operations are part of the front end.

3.1.2.2      Dynamic, Hackable Frontend Code




Mixing programming code in with HTML is messy.  We can talk about ways to format the code or structure your pages, but the end result will still be quite complicated.



We need to move as much of the code away from the HTML as possible.  But, we need to do this so that we don’t get lost in the interaction between our application and the user interface.



A web site is a dynamic target.  It is continually evolving, improving and changing.  We need to keep our HTML pages simple so that these changes can be made quickly and easily.  The best way to do that is by making all calls to PHP code simple and their results obvious.



We shouldn’t worry too much about the structure of the PHP code contained in the front end, it will change soon anyway.



That means that we need to remove all structured code from the actual pages into the supporting include files.  All common operations should be encapsulated into functions contained in the backend.





3.1.2.3      Stable, Structured Backend Code




In complete contrast to the web pages your backend code should be well designed, documented and structured.  All the time you invest here is well spent, next time you need a page quickly hacked together all the hard parts will be already done waiting for you in backend functions.



Your backend code should be arranged into a set of include files.  These should be either included dynamically when required, or automatically included in all pages through the use of the php3_auto_prepend_file directive.



If you need to include HTML in your backend code it should be as generic as possible.  All presentation and layout should really be contained in the front end code.  Exceptions to this rule are obvious when they arise, for example, the creation of select boxes for a date selection form.



PHP is flexible enough to let you design your code using classes and or functions.  My object oriented background means that I like to create a class to represent each facet of the application.  All database queries are encapsulated in these classes, hidden from the front end pages completely.  This helps by keeping all database code in a single location and simplifying the PHP code contained in pages.



3.1.3     Coding Techniques




3.1.3.1      Include Files




If we are building these function libraries we need to work out a scheme for including them in our pages.  There are a couple of different approaches to this.



We can either include all our library files all the time, or include them conditionally as required.



As part of my speed of coding philosophy I prefer to just include all the files and never think about it again.  When the Zend optimizing engine becomes available to pre-parse this code the performance hit will not be significant.



I have about 10,000 lines of code in PHP libraries for my site.  A quick check using the timing functions will tell us the damage:



<?php

require(‘timing.inc’);

ss_timing_start();

// include other library files here

ss_timing_stop();

echo ‘<h1>’.ss_timing_current().’</h1>’;

?>



It seems to take about 0.6 seconds to parse all my function libraries.  My sites do not receive millions of hits so this penalty is not important enough to worry about yet.



One drawback of including all libraries all the time is that it makes it difficult to work on them.  One mistake in any of those files will bring down every page on the entire site.  Be very, very careful.



If you are not as lazy as me then perhaps you’d prefer the conditional include technique.  It’s simple to use and implement.  Just structure all of your library files like the example below:



<?php // liba.inc



if ( defined( '__LIBA_INC' ) ) return;

define( '__LIBA_INC', 1 );



/*

 * Library code here.

 */



?>


Then you just need to include this library in any script where it is used.  Libraries may also need to include other libraries.  Your include statements look the same as normal:



include(‘liba.inc’);



This way, the calling scripts don't have to do any of the work.  Unfortunately return won't work from require()d files in PHP4 anymore. So, you will need to use include() instead.  You can still use require() in PHP3.





3.1.3.2      Design Patterns for Web Programming




Some of the best web programming techniques are captured in the Web Programming Design Patterns.  They are high level descriptions of the best solutions to common web programming problems.  You can read more about these here:



http://www.e-gineer.com/articles/design-patterns-in-web-programming.phtml



4         Speed of Coding, Not Speed of Code




4.1.1     Introduction




The hardest thing for me to learn as a web programmer was to change the way I wrote code.  Coming from a product development and university background the emphasis is on doing it the right way.  Products have to be as close to perfect as possible before release.  School assignments need to be perfect.



The web is different.  Here it is more important to finish a project as soon as possible than it is to get it perfect first time. Web sites are evolutionary, there is no freeze date after which it is difficult to make changes.



I like to think of my web sites as prototypes.  Everyday they get a little closer to being finished.  I can throw together 3 pages in the time it would take to do one perfectly.  It’s usually better on the web to release all three and then decide where your priorities lie.  Speed is all important.



So, everything you do as a programmer should be focused on the speed at which you are producing code (pages).




4.1.2     Optimizations to Satisfy Your Hacker Instinct




4.1.2.1      Introduction




This section describes some tricks you can use to speed up your PHP code.  Most of them make very little difference when compared to the time taken for parsing, database queries and sending data down a modem.



They are useful to know both so you can feel you are optimizing your code and to aid your understanding of certain PHP concepts.





4.1.2.2      Use Inline Tags Instead of echo




The PHP interpreter gets invoked once for each page.  Whatever is not contained in PHP tags like <? ?> is just echoed back out by the interpreter.



As a result it is faster to use lots of little in-line tags than it is to build massive strings or use echo statements.



Let’s use the timing functions we developed above to run a quick test.



<h2>Test Inline Tags vs echo</h2>



<p>



<?php ss_timing_start('echo'); ?>

<?php

for ($i=0; $i<1000; $i++) {

    echo $i."<br>";

}

?>

<?php ss_timing_stop('echo'); ?>



<p>



<?php ss_timing_start(str); ?>

<?php

$str = '';

for ($i=0; $i<1000; $i++) {

    $str .= $i."<br>";

}

echo $str;

?>

<?php ss_timing_stop(str); ?>



<p>


<?php ss_timing_start(inline); ?>

<?php

for ($i=0; $i<1000; $i++) {

?>

123<br>

<?php

}

?>

<?php ss_timing_stop(inline); ?>



<p>

<br>



<h2>Results</h2>



echo - <?php echo ss_timing_current('echo') ?>



<p>



str - <?php echo ss_timing_current(str) ?>



<p>



inline - <?php echo ss_timing_current(inline) ?>



The results of this test averaged out to be:



echo   - 0.063347 secs

str    - 0.083996 secs

inline - 0.035276 secs



We can see that inline is clearly the fastest technique.  But, when we consider that we only save 0.03 milliseconds each time we use it, the method you use to echo your values is pretty much irrelevant. A moral victory at best…





4.1.2.3      str_replace vs ereg_replace




It’s predictable that the simple str_replace() will be significantly faster than ereg_replace.  A quick test also reveals the time difference when we introduce a simple pattern match into the ereg_replace.



<h2>Test str_replace vs ereg</h2>



<p>



<?php $string = 'Testing with <i>emphasis</i>'; ?>


<?php ss_timing_start('str_replace'); ?>

<?php

for ($i=0; $i<1000; $i++) {

    str_replace('i>', 'b>', $string).'<br>';

}

?>

<?php ss_timing_stop('str_replace'); ?>



<p>



<?php ss_timing_start(ereg); ?>

<?php

for ($i=0; $i<1000; $i++) {

    ereg_replace('i>', 'b>', $string).'<br>';

}

?>

<?php ss_timing_stop(ereg); ?>



<p>



<?php ss_timing_start(ereg_pattern); ?>

<?php

for ($i=0; $i<1000; $i++) {

    ereg_replace('<([/]*)i>', '<\1b>', $string).'<br>';

}

?>

<?php ss_timing_stop(ereg_pattern); ?>



<p>

<br>



<h2>Results</h2>



str_replace - <?php echo ss_timing_current(str_replace) ?>



<p>



ereg - <?php echo ss_timing_current(ereg) ?>



<p>



ereg_pattern - <?php echo ss_timing_current(ereg_pattern) ?>



Here are the results.  Notice how using the simple pattern in ereg_replace has almost doubled the execution time.



str_replace - 0.089757

ereg - 0.149406

ereg_pattern - 0.248881



Again, the difference of these functions relative to one another is noticable but in the context of returning a web page basically irrelevant.





4.1.2.4      Quoted Strings




PHP parses double quoted strings to look for variables.  Any variable contained in a double quoted string will be resolved and inserted into the string at that location.



Single quoted strings are printed exactly as they appear.  They are not parsed.



So, you should use single quoted strings where possible to reduce the work to be done by the parser.



4.1.3     Optimizations that Really Make a Difference




4.1.3.1      Reduce queries




Accessing the database is expensive.  Persistent connections reduce a lot of the overhead by removing the need to connect with each request, but performing queries is still a high cost exercise compared with the execution of PHP code.



This is particularly true due to locking issues in the database.  In testing you might see that individual queries to the database are actually quite fast.  In production you will see the database get overloaded with many small queries as it struggles to satisfy a single large query.





4.1.3.2      Optimize your Queries




The type of queries you make to the database will have a dramatic effect on the speed of your application.  Making smart use of column indexes is essential.  Small changes to your SQL can result in dramatic time savings.





4.1.3.3      Avoid joins




Joins are expensive.  The minute you do a join the size of the resulting table becomes the multiple of the tables being joined.



Lets look at some quick statistics to give you a feel for the cost of joins.  I have created two tables, foo and big_foo.  Foo contains a single column with the numbers 1-1000.  Big_foo contains a single column with the numbers 1-1,000,000.  So, big_foo is equivalent in size to the join of foo with itself.



$db->query(“select * from foo”);

0.032273 secs



$db->next_record();

0.00048999999999999 secs

$db->query(“insert into foo values (NULL)”);

0.019506 secs



$db->query(“select * from foo as a, foo as b”);

17.280596 secs



$db->query(“select * from foo as a, foo as b where a.id > b.id”);

14.645251 secs



$db->query(“select * from foo as a, foo as b where a.id = b.id”);

0.041269 secs



$db->query(“select * from big_foo”);

25.393672 secs



We can see from the results above that selecting all rows from the join of a 1000 row table is only marginally quicker than selecting all rows from a 1,000,000 row table.



It is worth noting that a join that returns a small number of rows is still very fast.





4.1.3.4      Make your pages smaller




Let’s consider a typical user on a 56Kbps modem.  On a good connection, they can download pages at approximately 6kBps.  We were looking at optimizations above that save approximately 0.15 seconds on an extremely complex page.  Reducing the size of your page by about 900 bytes will give you an equivalent saving.



Usually response times for the user are gained most easily through examination of your HTML and optimizing the use of images.



4.1.4     Gotchas




4.1.4.1      Introduction




This section will be quite small since most PHP developers are now making the switch to PHP4.  The parsing engine has been completely rewritten and has removed a lot of the annoying quirks that can bring a lot of grief to both newcomers and experienced PHP programmers.





4.1.4.2      Arrays of Objects




PHP3 does not handle arrays of objects very well.  The following code will NOT parse correctly:



$a[$i]->foo();



PHP does not like the object reference after the array index brackets.  Instead you need to use a temporary variable:



$tmp = $a[$i];

$tmp->foo();





4.1.4.3      Calling overridden methods




PHP3 has support for classes and inheritence.  You can even override functions in subclasses.  Problems occur when you need to call the overridden function in the parent class.  Unfortunately this is quite common as you may want to define the function in the subclass as being the original function plus some extra work.  If that explanation has made you completely confused take a look at the example below.



There is a (hacky) work around.  The basic idea is to define a unique method name in each class for the same method.  Then the extended class can reference directly to the unique method name in its parent.



To achieve the appearance of polymorphism when using the class you just create a method with the desired name in every class definition that calls the unique method name in that class.  An example will explain it better:



class A {

  function A() { }



  function A_dspTwo() {

    echo "A: Two<br>";

  }



  function dspTwo() {

    return $this->A_dspTwo();  // call the class A dspTwo method

  }

}

 

class B extends A {

  function B() {

    $this->A(); // call the parent constructor.

  }



  function B_dspTwo() {

    $this->A_dspTwo();

    echo "B: Two<br>";

  }



  function dspTwo() {

    return $this->B_dspTwo();

  }

}



$object = new B();

$object->dspTwo();

This is supported by the Zend engine and will thus be supported in PHP 4.0.





4.1.4.4      Trouble with Types




PHP is a loosely typed language.  That means that the variables actually do have types, but in general you do not need to worry about them.  PHP will automatically convert variables between types when required.



Unfortunately there are some cases where you need to manually convert the type of variables.  This can lead to confusion because they are very rare.  Below is an example page to highlight how rare these cases can be:



<h2>Test String Integer Comparisons</h2>



<?php

$a = 1;

$b = '2';

if ($a < $b) {

    echo ss_as_string($a).' < '.ss_as_string($b);

}

else {

    echo ss_as_string($a).' >= '.ss_as_string($b);

}

?>



<p>



<?php

$a = 2;

$b = '2';

if ($a == $b) {

    echo ss_as_string($a).' == '.ss_as_string($b);

}

else {

    echo ss_as_string($a).' != '.ss_as_string($b);

}

?>



<p>



<?php

$a = array(2, '1');

if ($a[0] > $a[1]) {

    echo ss_as_string($a[0]).' > '.ss_as_string($a[1]);

}

else {

    echo ss_as_string($a[0]).' <= '.ss_as_string($a[1]);

}

?>



<p>




<?

$a = array('2', '1');

echo ss_as_string($a).'<br>sorts to<br>';

sort($a);

echo ss_as_string($a);

?>



<p>



<?

$a = array(2, 1);

echo ss_as_string($a).'<br>sorts to<br>';

sort($a);

echo ss_as_string($a);

?>



<p>



<?

$a = array('2', 1);

echo ss_as_string($a).'<br>sorts to<br>';

sort($a);

echo ss_as_string($a);

?>



<p>



<?

$a = array(2, '1');

echo ss_as_string($a).'<br>sorts to<br>';

sort($a);

echo ss_as_string($a);

?>



Here is the output from these tests.  Notice that all the tests work correctly except for the last one, sorting array(2, ‘1’).  We can even sort array(‘2’, 1) without problems.  The error occurs when we have multiple types in an array passed to the sort function with the order number then string.



Long(1) < String(2)



Long(2) == String(2)



Long(2) > String(1)



Array(
0 ==> String(2)
1 ==> String(1)
)
sorts to
Array(
0 ==> String(1)
1 ==> String(2)
)



Array(
0 ==> Long(2)
1 ==> Long(1)
)
sorts to
Array(
0 ==> Long(1)
1 ==> Long(2)
)



Array(
0 ==> String(2)
1 ==> Long(1)
)
sorts to
Array(
0 ==> Long(1)
1 ==> String(2)
)



Array(
0 ==> Long(2)
1 ==> String(1)
)
sorts to
Array(
0 ==> Long(2)
1 ==> String(1)
)



4.1.5     Tricky Concepts




4.1.5.1      Include vs Require




Include() and require() are slightly different.  Basically, include is conditional and require is not.



This would include 'somefile' if $something is true:



if($something){

    include("somefile");

}



This would include 'somefile' unconditionally



if($something){

    require("somefile");

}




This would have VERY strange effects if somefile looked like:



} echo "Ha!  I'm here regardless of something: $something<br>\n";

if (false) {



Another interesting example is to consider what will happen if you use include() or require() inside a loop.



$i = 1;

while ($i < 3) {

    require(“somefile.$i”);

    $i++;

}



Using require() as above will cause the same file to be used every single iteration.  Clearly this is not the intention since the file name should be changing in each iteration of the loop.  We need to use include() as below.  Include() will be evaluated at each iteration of the loop including somefile.0, somefile.1, etc as expected.



$i = 1;

while ($i < 3) {

    include(“somefile.$i”);

    $i++;

}



The only interesting question that remains is what file will be required above.  It turns out that PHP uses the value of $i when it reads the require() statement for the first time.  So, the require() loop above will include something.1 two times.  The include() loop includes something.1 and something.2.





4.1.5.2      Echo vs Print




There is a difference between the two, but speed-wise it should be irrelevant which one you use.  print() behaves like a function in that you can do:



$ret = print "Hello World";



and $ret will be 1.



That means that print can be used as part of a more complex expression where echo cannot.  print is also part of the precedence table which it needs to be if it is to be used

within a complex expression.  It is just about at the bottom of the precendence list though.  Only "," AND, OR and XOR are lower.



echo is marginally faster since it doesn't set a return value if you really want to get down to the nitty gritty.




If the grammar is:



echo expression [, expression[, expression] ... ]



Then



echo ( expression, expression )



is not valid.  ( expression ) reduces to just an expression so this would be valid:



echo ("howdy"),("partner");



but you would simply write this as:



echo "howdy","partner";



if you wanted to use two expressions.  Putting the brackets in there serves no purpose since there is no operator precendence issue with a single expression like that.



4.1.6     Scripting with PHP




It’s easy to forget that PHP is a complete programming language that can be used for more than just generating web pages.  I was once writing a script to receive emails and place them in a database.  I was fumbling around in Perl and shell scripts until it dawned on me to install PHP for scripting.  30 minutes later the emails were churning in.



Installing PHP for scripting on unix is easy.  Just remove the –with-apache directive from your configure options.  This will create the PHP binary that can be used to run scripts directly from the command line.  There are complete instructions for installing PHP for scripting here:



http://www.e-gineer.com/instructions



You can then write your script like any other shell script.  Here is an example:



#!/usr/local/bin/php –q

<?php

// your php code here

?>



Once you start scripting with PHP the possibilities are endless.  It’s a fully featured language, you can do anything you would normally do in a shell script.




4.1.7     Extreme Programming




We are getting a little off topic here, but I believe programming techniques are an important part of being a good programmer.



My working style is based on the ideas of Extreme Programming.  From the Extreme Programming web site:



XP improves a software project in four essential ways; communication, simplicity, feedback, and courage. XP programmers communicate with their customers and fellow programmers. They keep their design simple and clean. They get feedback by testing their software starting on day one. They deliver the system to the customers as early as possible and implement changes as suggested. With this foundation XP programmers are able to courageously respond to changing requirements and technology.



The focus on speed and change is what makes Extreme Programming so suitable for web projects.



You can learn more about Extreme Programming here:



http://www.extremeprogramming.org



5         Getting Help




There are a number of resources available for PHP help.  The PHP community is generous with its time and assistance.  Make use of their contributions and use the time you save to help others.



The PHP Knowledge Base is a growing collection of PHP related information.  It captures the knowledge from the mailing list into a complete collection of searchable, correct answers.  Of course, I may be a little biased.






The PHP manual is a great reference point for information on functions or language constructs.







If you can’t find the relevant information in the PHP Knowledge Base your next stop should be the mailing list archives.  There are thousands of questions on the mailing list every month so you can be almost certain your question has been asked before.  Prepare to do some wading.






If all that searching fails to help, try asking on the mailing list.  A lot of PHP gurus reside there.






If all these on-line resources aren’t enough or you hate reading from a computer screen, you might be interested in one of the many PHP books that are now available.





domingo, 13 de diciembre de 2015

seguridad


sábado, 5 de diciembre de 2015

HACKING MEXICO


La humildad es para el que quiere la gloria, sin parecer un patan.

Normalmente toda la gente que consideran "humilde" y ha triunfado, solo son personas hipocritas que quieren dar una buena cara ante el publico.

Quieren mostrarse caritativos o filantropos, pero siempre y cuando haya un medio que los filme haciendo un donativo.
...

Si realmente fueran desinteresados y humildes como dicen, donarian anonimamente, sin necesidad de alzar la mano y decir "miren que buena persona soy"

El ser "humilde" es falso, en realidad nadie lo es, solo gustan de la atencion brindada, y cuando los demas les dicen "que bueno que eres humilde" es una inyeccion de ego.

Todos los que dicen "deberias ser mas humilde" es por que les duele ver que otras personas tengan mas.

Aquellos que se paran el culo diciendo "No tengo por que demostrar o presumirte nada" no es por humildes, es por que no tienen con que.

El dinero no pudre a las personas, siempre han estado podridas, pero no tenian con que demostrarlo.




TRIBUTO A HACKING MEXICO

Por que me gusta el Hacking?


Por que soy un sociopata narcisista altamente funcional, que gusta tener el control sobre los secretos de los demas.


Mi arma es el mouse, la red mi religion. El poder controlar tu ordenador: mi satisfaccion....

No me interesa lo que escondes, me interesa hasta donde llegarias por que nadie mas lo sepa.

Comprendo cosas, que no tienen sentido para ti, me fascina encontrar patrones dentro de paquetes de trafico capturado. Llegar hasta donde nadie mas ha llegado.

Me gusta tenderte trampas, jugar con tu inteligencia como si fueras un simio amaestrado, genial, ya le diste click….

Me divierte analizar tus predecibles actos, que musica te gusta, cual es tu cantante favorito, tu color preferido, el password es RedBonJovi

Yo soy el hombre de en medio, mi nombre es byte, Mega-byte.

M.C Raul Robles







Como aprendi Hacking?



Mucha gente es lo que me pregunta, algunos piensan que en la Universidad, o en la maestria, pero en realidad de la educacion academica solo aprendi razonamiento, matematicas y muy poco de Informatica o Sistemas. Al inicio de los primeros semestres llevas 5 materias de matematicas y fisica, y solo 1 de programacion.



Realmente no tuve maestros informaticos, lo que me hacia aprender era la curiosidad y el autoestudio. Ahora paso por los foros o grupos de Facebook y veo gente preguntando que es FAT32 o que es un Bitcoin? Y me pregunto si realmente nunca aprendieron a usar un buscador o solo lo hacen por convivir.

Creo que lo que me ayudo mas es que siempre he sido un personaje polemico, por lo que en todos los foros de la epoca, cyruxnet, elhacker, hackxcrack, neosecurityteam, underground, etc siempre estaba baneado, y jamas pude pedir ayuda directamente, ya que como se veria que “Megabyte” solicitara ayuda en un foro, si el dice saberlo todo.

El tener las puertas cerradas en las comunidades de habla hispana al menos activamente, me ayudo para buscar en otros medios, foros gringos, rusos, y brazileños, y sobretodo hacer pruebas yo mismo, creo que siempre me base en la frase que dice “Fake it, until you make it” A los 15 años que inicie en esto, no sabia muchas cosas, sin embargo mi actitud siempre fue de sabelotodo, si algo no sabia, lo investigaba.

Mi facilidad para aprender siempre habia sido uno de mis dones, memoria fotografica, capaz de entender proceso complejos a la primera.

Lo que mas me llamo la atencion fue un dia que andaba de lammer floodeando un chat de burundis, cuando otro lammer me dijo que iba a tomar control de mi mouse, y que lo moveria remotamente. Obviamente no sucedio, pero esa fue la gota que derramo el vaso, eso fue lo que me hizo investigar si aquella hazaña era posible o solo un cuento.

Mi primer encuentro con los Troyanos: Netbus, DeepThroat,Evil Witch, SubSeven,Back Oriffice, recuerdo que cuando los descargue venian en formato .zip y no sabia como abrirlos (en ese entonces Windows no tenia descompresor de archivos)

Ya sabia usar Outlook para enviar correos, asi que decidi contactar al dueño de la pagina gringa de donde baje los Troyanos, admin@smarthack.com, y cual fue mi sorpresa que me respondio a las pocas horas diciendo que probara con “Winzip”

Aprendi a buscar la informacion que necesitaba en Google, aprendi a entrar a usenet y sus grupos de noticias, me cree mi propia pagina en Geocities donde me proclamaba como Hacker. Y apartir de ahi empeze a buscar formas de atacar otras paginas de Seguridad, o de Hackers que estaban de moda, o con muchas visitas, fui en contra de todos los foros donde me odiaban, y con exito logre defacearlos, el odio incremento.

Hay personas que hoy en dia despues de 15 años siguen ardidos por esas acciones que realize cuando era un adolescente.

Pero en fin, a las nuevas generaciones les digo, investiguen por su cuenta, hagan pruebas, no van a aprender de libros, hasta que no practiquen.

Y si no tienes el ingenio, la curiosidad, la maldad por asi decirlo, podras saber usar 1000 herramientas y 1000 tecnicas, pero jamas podras destacar, esto se tiene o no se tiene, nadie te lo puede enseñar.

M.C Raul Robles
 Zone-H Top 100 Defacers 1999-2003






deep WEB 2

Lo sabemos porque nos pasó lo mismo. Desde que sabes que internet no termina ahí nomás, y que tiene un fondo de océano que parece la metáfora de una película posapocalíptica, tu visión sobre ella ha cambiado. Como sabemos que las posibilidades que se abren en la Deep Web y en todo lo que se esconde ahí son infinitas, te daremos una llave al descubrimiento a cambio de tu promesa de responsabilidad de uso. A continuación veremos cómo acceder a la Deep Web con Tor, consiguiendo el anonimato, la privacidad y la seguridad necesaria como para navegar y conocer este nuevo/viejo Mundo subterráneo. 

Introducción

La semana anterior nos dimos, con responsabilidad mayúscula, el lujo de acercarle a muchos una información que sólo sabe un 5% de los internautas del mundo (y tal vez estoy siendo generoso). La deep Web se abrió ante los ojos de muchos curiosos lectores que intuían que las fronteras estaban más allá de lo que los motores de búsqueda nos ofrecían, y habiendo tomado la pastilla roja para adentrarse en un mundo subterráneo donde la información disponible de fuentes desconocidas que podrían hacerte ganar un Pullitzer se mezclan con oscuras tramoyas que podrían hacerte visitar la cárcel un tiempo o, al menos, pasarla mal accediendo o visualizando contenido bastante perturbador. Como prometimos, la Deep Web tiene algunos canales de entrada cuyas reglas hay que respetar, pero igualmente te guiaremos en el proceso de descarga y configuración de una de las herramientas para ingresar a laDeep Web anónimamente y experimentar, por tu propia cuenta y responsabilidad, lo que son las profundas alcantarillas de la web que el vulgo conoce. Pero antes, algunas aclaraciones imprescindibles.
Cómo acceder a la Deep Web con Tor
Cómo acceder a la Deep Web con Tor

Advertencia

Habiéndolo aclarado en la anterior entrega, la repetición de la advertencia  no es redundante, puesto que el tema es importante y se debe demostrar un grado de maduración aceptable para conllevar esta experiencia sin consecuencias legales, sociales o psicológicas. Es por lo tanto que advertimos que lo que te puedes encontrar en laDeep Web es totalmente azaroso en las primeras incursiones, pues por puro desconocimiento, puedes pasar de buscar información sobre base de datos o métodos para hacer más anónima tu conexión a lidiar con pedofilia, ventas de armas, malware, drogas, etc. Por supuesto, desde Neoteo no alentamos este tipo de actividades en la red y las rechazamos fuertemente. En primer lugar porque pueden ser perjudiciales para ti en muchos sentidos, y en segundo lugar porque son ilegales, además de contribuyentes a que mercados que generan trastornos, violencia y muerte se sigan expandiendo endémicamente.
¿Entonces para qué ponen una guía sobre cómo acceder a la Deep Web?
El conocimiento. Conocer más sobre el mundo que nos rodea (o habita bajo nuestro) nos prepara para responder con más herramientas ante la amenaza que pudiera significar. La ignorancia es contraria de la libertad, pues al no conocer todas las posibilidades de elección, la elección queda recortada al pequeño paquete de opciones entre las que “quieren” que elijas, generando la falsa ilusión de la libertad de elección.
Resulta que la parte oscura de la Deep Web es meramente una parte, lo que quiere decir que el ingreso a la Deep Web tiene ventajas sustanciales a la hora de comunicarse anónimamente para denunciar hechos, proteger la privacidad y la libertad en situaciones peligrosas, además de conseguir información legal que no va por los canales oficiales (imaginen una situación de opresión estatal, dictadura, expresión cercenada, etc.). Ergo, no por entrar a laDeep Web vas a ir directo a comprar drogas o armas. Y aunque ésta sería una elección propia de tu libertad, no está en nosotros juzgarte, sino advertir que no es nuestra intención promover esas prácticas ni otras peores por su carácter de ilegal y peligrosas. Dicho esto, es hora de que vuelvas a elegir entre el seguro camino de la ignorancia o el riesgo del saber un poco más. Depende de ti.

Tor y el anonimato

Como anticipamos en el anterior artículo y en este, el método que utilizaremos para acceder a la vastedad de la Deep Web es uno de los más comunes para los principiantes en la navegación segura o anónima. Este último término será fundamental en todo el proceso de aprendizaje y práctica, pues en la Deep Web hay una regla fundamental: No entres sin una IP anónima. Esto se debe a lo comentado en la advertencia, pues así como pasa en la web superficial pero con menos gravedad, un clic mal hecho puede llevarte a contenido que te podría meter en problemas con la ley. Por esto, la motivación de una aplicación como Tor es la de generar un marco de navegación seguro, anónimo y por lo tanto privado que sea inmune (o casi, ya lo veremos) al trabajo de los análisis de tráfico que llevan a cabo diferentes instituciones de seguridad estatal o privada, así como sitios web que detectan tu locación física y prohíben el acceso al contenido.
Tor funciona  entonces como un canal de comunicación privado que genera un anonimato parcial en base a cifrado y descifrado de información en los envíos y recepciones de información entre el origen y el destino (donde el contenido, no la identidad, puede quedar expuesto a los administradores del servidor), redirigiendo la comunicación entre diferentes nodos TOR (OR, de Onion Router, protocolo al que hacer referencia Tor) para evitar su rastreo.

Instalación de Tor

Otra advertencia necesaria es que este tutorial es para principiantes, por lo que no veremos formas avanzadas de acceso, configuración de relay, modificación nódica, simbiosis con otros ocultadores SSL o de Proxy, verificación de firmas, etcétera que no sólo expandirían este informe a una biblia, sino que confundirían. Por eso nos abocaremos únicamente a Tor y a una configuración óptima como para navegar (no descargar ni visualizar contenido multimedia, salvo imágenes) las partes más blandas de la Deep Web sin correr riesgos. También -lo reconozco- como un mecanismo de protección hacia los lectores, pues quien quiera acceder a contenido más complicado en términos legales, tendrá que hacer su propia investigación personal sopesando y asumiendo los riesgos del caso.
Para empezar, lo que comúnmente puedes utilizar es el Tor Browser Bundle, que está disponible para Windows, Mac, Linux y es un navegador preconfigurado para tener anonimato de nivel seguro estándar sin una configuración avanzada demasiado abrumadora. Una vez que lo hayas descargado a tu ordenador o puesto en un pendrive (es portátil y no requiere instalación), ejecutas el archivo descargado y verás que tendrás un archivo para descomprimir. Lo descomprimes y ya estará listo para ejecutarse junto a tu navegador a través de Start Tor Browser.

Configuración de Tor

A continuación se abrirá el Panel de Control de Vidalia (que te dará opciones ver el gasto de ancho de banda, ver logs, activar proxies, modificar directorios, etc.) y luego de que se conecte automáticamente a la red de Tor, se ejecutará el navegador (Aurora de Firefox, en mi caso) indicando con letras verdes que ya estás conectado. En ella nos dará una dirección IP anónima y ya estaremos prontos para comenzar a configurar más seriamente otras funciones en caso de querer profundizar el anonimato y la seguridad. 
Como dijimos arriba, Tor puede hacer anónimo el origen de tu tráfico web y encriptarlo dentro de la red Tor, pero no puede cifrar tu tráfico entre la red Tor y el destino final del tráfico. Para que esta segunda parte tenga solución, tendrás que ir a por un asegurador de HTTPS como HTTPS Everywhere, que es una extensión para Firefox que puede funcionar junto a Tor (igualmente ya viene instalada por defecto, pero la mencionamos por si hacen configuraciones manuales). Para adherir un proxy más potente, puedes probar con Privoxy y configurarlo junto a Tor. Esto es por si quieres descargar archivos de la Deep Web con un nivel excesivo de seguridad, algo que en este informe no tocaremos. Fuera de esto, Tor es potente en sí mismo, y además de tener un administrador de cookies, proxy y plugins propio y eficiente, posee también un botón para cambiar la identidad (otra IP) desde el navegador.

Algunos consejos para el uso de Tor

Al comenzar a utilizar Tor como tu navegador anónimo por defecto debes tener en cuenta que la navegación en él será mucho más lenta de lo que experimentas regularmente, por lo que no desesperes si algunos sitios (sobre todo los de la Deep Web) demoran casi un minuto o más en responder.
Otra advertencia en el uso de Tor es que no debes utilizarlo junto  a BitTorrent, pues el entrecruzamiento de puertos y de cifrado podría anular el anonimato.
Otra cuestión importante es que si ingresas a un sitio sin Tor y éste deja una cookie en tu sistema, al volver a ingresar al sitio pero con Tor, igualmente podrás ser seguido a través de la cookie. Para esto lo mejor es borrar las cookies con alguno de los programas que siempre recomendamos.
Por último te recordamos que Tor deshabilita por defecto la mayoría de los scripts, plugins y codecs para navegadores como Flash, Java, Active X, Adobe, Quicktime, etc. Sin embargo, desde un botón en la barra del navegador con Tor podrás activarles en caso de necesidad, aunque, por supuesto, no es recomendable porque no son tecnologías muy amigas del anonimato. Lo mismo al reproducir archivos de audio, vídeo o descargar archivos comprimidos, te recomendamos especial atención y una evaluación seria de los riesgos que tiene descargar archivos sin ningún tipo de seguridad y con la posibilidad de que te desenmascare ante algún dispositivo de vigilancia, benigno o maligno.

Dominios .onion

En Tor podrás comenzar una navegación anónima simplemente introduciendo cualquier dirección o IP de la web superficial de todos los días, pero no nos engañemos más y pasemos a lo que realmente llama la atención en todo este asunto de la Deep Web. La entrada propiamente dicha a la parte más invisible de la Deep Web se realiza a través de lo que se llama Onionland, o también -mal- denominada Darknet. Estos son sitios reglados bajo dominios del tipo .onion que tienen la particularidad de no revelar el título de su contenido en su dirección (ej. Facebook.com), sino que se lo esconde con URLs del tipo kj369k76352k3hte.onion (16 caracteres alfa numéricos). Este protocolo (Onion Router) que Tor toma como base para su funcionamiento, será el que presente los sitios más interesantes en cuanto a anonimato en la deep web. Para acceder a ellos tendrás que saber su IP e introducirla en Tor a través de la barra de direcciones.

¿Qué hay en Onionland?

Entre los sitios que podemos recomendar como positivos para las actividades que requieran privacidad está el motor de búsquedas Torch. También Tordir (un sistema de enlaces, mensajes privados y más para Tor), Anonymoose Chat (una sala de conversación cifrada) y algunos tablones de imágenes o sitios de discusión y debate como Talk. También hay bases de datos de libros, sitios para denuncias de corrupción o cuestiones políticas (WikiLeaks), a lo que se le suman los foros, foros y más foros de discusión totalmente libres.
Por otro lado existen los sitios llamados Hidden Services, donde se comercializan todo tipo de productos o servicios legales e ilegales, y donde la moneda que se utiliza mayoritariamente es el Bitcoin, que ya hemos cubierto en profundidad. Para ambos universos de contenido, la enciclopedia del bajo mundo tiene el nombre de Hidden Wiki, y desde ella podrás acceder a un tutorial de cómo encontrar todo lo que buscas en la Deep Web de manera segura y un listado de sitios que se pueden encontrar en la Deep Web bajo el protocolo de Onionland.

Epílogo

Esperamos que este pequeño y básico informe sobre cómo acceder a la Deep Web con Tor les haya sido de ayuda para saber un poco más sobre la red y tener una herramienta de investigación, búsqueda y opinión invalorable a tu alcance. Otra vez, creemos que los lectores de Neoteo (y de los sitios que replican nuestros artículos) son personas maduras y responsables de sus acciones, por lo tanto el llamamiento es a que utilicen, si les importa, la Deep Web  y el anonimato a través de Tor para protegerse en sus actividades (sea por gusto, curiosidad, necesidad, etc.) cotidianas sin ponerse en peligro y sin contribuir a poner en peligro a otras personas. El conocimiento es poder, úsalo con sabiduría y responsabilidad.

DEEP WEB



Qué onda youtubers, bueno en este video les muestro como descargar este famoso navegador que es uno de los más utilizados para navegar con anonimato por la web y también es utilizado para entrar a la deep web, si eres nuevo en este tema, te recomiendo buscar mas información sobre este tema para que estés informado, puedes checar videos sobre la deep web o información en Internet... NO ME HAGO RESPONSABLE DEL MAL USO DE ESTA INFORMACIÓN O SI LES LLEGARA A OCURRIR ALGO AL ENTRAR A LA DEEP WEB, USTEDES ENTRAN BAJO SU PROPIA RESPONSABILIDAD

▀▄▀▄▀▄▀▄▀▄▀▄▀▄▀▄▀▄▀▄▀▄▀▄▀▄▀

Al darle clic al enlace de TOR te mandara a una pagina con publicidad, solo tienes que esperar 5 segundos y después en la parte superior derecha te saldrá un botón que dice saltar publicidad, le das clic y listo:Si tienes un bloqueador de publicidad, desactivalo temporalmente

El tor se a actualizado, tal ves no se vea como en el video, pero es básicamente lo mismo :D
Link TOR:http://adf.ly/YlnMp

Ahora bien, para los que quieran la versión desactualizada de Tor que sale en el video, aquí les dejo el link de descarga por mega (es para que les salga igual la ventana del minuto 2:59):
http://adf.ly/cYolg

▀▄▀▄▀▄▀▄▀▄▀▄▀▄▀▄▀▄▀▄▀▄▀▄▀▄▀

Nuevo Link de la hidden wiki 2015: http://zqktlwi4fecvo6ri.onion

Link de la hidden wiki: kpvz7ki2v5agwt35.onion

Para más información de la Deep Web:
http://goo.gl/x6Uvhc

Sitios en la Deep Web que podrían interesarte:
http://adf.ly/1L6YRs

▀▄▀▄▀▄▀▄▀▄▀▄▀▄▀▄▀▄▀▄▀▄▀▄▀▄▀
IMPORTANTE: Al parecer varios links de la deep web están callendo y no se sabe cuándo vallan a estar en funcionamiento, incluyendo el de la Hidden Wiki, pero aun así, aquí les dejo el link de una página donde hay unos enlaces alternativos de la Hidden Wiki

http://adf.ly/cYj2b

Espero les sea de ayuda....

Grupo en Facebook: http://adf.ly/nIanT

Nueva página web: http://adf.ly/nIaUJ

Mi pagina web: http://minitutospc.weebly.com/

Descarga mis extensiones para tu navegador:
GOOGLE CHROME:http://myapp.wips.com/franx-neko-exte...

MOZILLA FIREFOX:http://myapp.wips.com/franx-neko-addon

Esto de a qui abajo no tiene importancia ;D :

Como Descargar e Instalar TOR (Anonymous) Español
Como Usar Tor + Configuracion
Deep Web - Web profunda todo lo que debes saber - Proyecto Tor y dominios onion
Como usar Tor [HD]
Como Acceder A La Deep Web Usando Tor
Como entrar ala Deep Web usando Tor Browser
Tutorial Tor
como usar TOR Browser
Instalando y Configurando Tor
Como Usar Tor Configuración
como entrar a la deep web MODO SEGURO
Como entrar a la deep web y hidden wiki con tor
  • Categoría

  • Licencia

    • Licencia de YouTube estándar

miércoles, 15 de julio de 2015