Php

De 33system wiki
Saltar a: navegación, buscar

<< Página principal

php

Teoría

Práctica

Incluir un archivo .php

 include 'vars.php';

Cortar cadena de caracteres

$new=substr($string,0,10);

Evitar conexiones desde el exterior a un .php

if(preg_match("/".basename(__FILE__)."/i",$_SERVER['REQUEST_URI']))  
 {  
  die("Please contact with support@33system.com");  
 }

Consulta mysql en un array

 function db_connect() {
    include 'vars.php';
    $link
    or die('No se pudo conectar: ' . mysql_error());
    mysql_select_db('data') or die('No se pudo seleccionar la base de datos');

    $query = "SELECT data,data1 FROM data WHERE data='data' GROUP BY data ASC";
    $result = mysql_query($query) or die('Consulta fallida: ' . mysql_error());

    while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
          echo '<td>'.$row[0].'</td><td>'.$row[1].'</td>;
    }

    mysql_free_result($result);
    mysql_close($link);
 }

Insertar valores en la base de datos

function register_visit($id) {

        include 'vars.php';

        $link
        or die('No se pudo conectar: ' . mysql_error());
        mysql_select_db('database') or die('No se pudo seleccionar la base de datos');

        mysql_query("INSERT INTO tabla VALUES (DEFAULT,CURDATE(),CURTIME(),data,$id,'data'");

        mysql_close($link);
 }

Vanear una ip

<?php
$deny = array("111.111.111", "222.222.222", "333.333.333");
if (in_array ($_SERVER['REMOTE_ADDR'], $deny)) {
//redirigimos a google
   header("location: http://www.google.com/");
   exit();
} ?>

Obtener ip de una visita

function getUserIP()
{
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = $_SERVER['REMOTE_ADDR'];

    if(filter_var($client, FILTER_VALIDATE_IP))
    {
        $ip = $client;
    }
    elseif(filter_var($forward, FILTER_VALIDATE_IP))
    {
        $ip = $forward;
    }
    else
    {
        $ip = $remote;
    }

    return $ip;
}

Geolocalización de una ip

<?php

function ip_info($ip = NULL, $purpose = "location", $deep_detect = TRUE) {
    $output = NULL;
    if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
        $ip = $_SERVER["REMOTE_ADDR"];
        if ($deep_detect) {
            if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
                $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
            if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
                $ip = $_SERVER['HTTP_CLIENT_IP'];
        }
    }
    $purpose    = str_replace(array("name", "\n", "\t", " ", "-", "_"), NULL, strtolower(trim($purpose)));
    $support    = array("country", "countrycode", "state", "region", "city", "location", "address");
    $continents = array(
        "AF" => "Africa",
        "AN" => "Antarctica",
        "AS" => "Asia",
        "EU" => "Europe",
        "OC" => "Australia (Oceania)",
        "NA" => "North America",
        "SA" => "South America"
    );
    if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) {
        $ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip));
        if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) {
            switch ($purpose) {
                case "location":
                    $output = array(
                        "city"           => @$ipdat->geoplugin_city,
                        "state"          => @$ipdat->geoplugin_regionName,
                        "country"        => @$ipdat->geoplugin_countryName,
                        "country_code"   => @$ipdat->geoplugin_countryCode,
                        "continent"      => @$continents[strtoupper($ipdat->geoplugin_continentCode)],
                        "continent_code" => @$ipdat->geoplugin_continentCode
                    );
                    break;
                case "address":
                    $address = array($ipdat->geoplugin_countryName);
                    if (@strlen($ipdat->geoplugin_regionName) >= 1)
                        $address[] = $ipdat->geoplugin_regionName;
                    if (@strlen($ipdat->geoplugin_city) >= 1)
                        $address[] = $ipdat->geoplugin_city;
                    $output = implode(", ", array_reverse($address));
                    break;
                case "city":
                    $output = @$ipdat->geoplugin_city;
                    break;
                case "state":
                    $output = @$ipdat->geoplugin_regionName;
                    break;
                case "region":
                    $output = @$ipdat->geoplugin_regionName;
                    break;
                case "country":
                    $output = @$ipdat->geoplugin_countryName;
                    break;
                case "countrycode":
                    $output = @$ipdat->geoplugin_countryCode;
                    break;
            }
        }
    }
    return $output;
}

?>
  • uso
echo ip_info("Visitor", "Country");
echo ip_info("Visitor", "Country Code");
echo ip_info("Visitor", "State");
echo ip_info("Visitor", "City");
echo ip_info("Visitor", "Address");
echo ip_info("Visitor", "Location");

evitar acceso a php

if(preg_match("/".basename(__FILE__)."/i",$_SERVER['REQUEST_URI']))
 {
  die("Please contact with support@33system.com");
 }

Documentación