Войти на сайт ( )
[Вход] [Регистрация]
Мобильная связь и Интернет » Интернет-ресурсы » Полезные куски кодов [[deleted]]
[Обновить]
[ Ответить ]
[Добавить в закладки]
Страница: 1, 2, 3 ... , 18, 19, 20
[deleted] ( off )
( 08:57 19-02-2017 )
Выбор текста из списка

<html><head><metacharset="utf-8"></head>



<body><form><p><inputlist="cocktail"></p><datalistid="cocktail"><option>Аперитивы</option><option>Горячие</option><option>Десертные</option><option>Диджестивы</option><option>Молочные</option><option>Слоистые</option></datalist></form></body></html>

[deleted] ( off )
( 08:58 20-02-2017 )
Отдача файлов

function file_dload($file, $name = 'test.txt', $type = 'application/octet-stream', $del = 1) {

ob_end_clean();

ob_start();

header('Content-Description: File Transfer');

header('Content-Type: '.$type);

header('Content-Disposition: attachment; filename=' . $name);

header('Content-Transfer-Encoding: binary');

header('Expires: 0');

header('Cache-Control: must-revalidate');

header('Pragma: public');

header('Content-Length: ' . filesize($str));

readfile($file);

if($del)

unlink($file);

flush();

return true;

}

[deleted] ( off )
( 08:59 20-02-2017 )
Получение координат

<?php

function getLatLong($address){

if (!is_string($address))die("All Addresses

must be passed as a string");

$_url = sprintf('http://maps.google.com/maps?

output=js&q=%s',rawurlencode($address));

$_result = false;

if($_result = file_get_contents($_url)) {

if(strpos($_result,'errortips') > 1 || strpos

($_result,'Did you mean:') !== false) return

false;

preg_match('!center:s*{lat:s*(-?d+.d

+),lng:s*(-?d+.d+)}!U', $_result,

$_match);

$_coords['lat'] = $_match[1];

$_coords['long'] = $_match[2];

}

return $_coords;

}

?>

[deleted] ( off )
( 09:01 20-02-2017 )
Перемешивание массивов

function sort_array_rand($array) {

$result = array();

$y = sizeof($array);

$x = 0;

while ($x<pow(2, $y)+1) {

$result[] = $array[array_rand($array)];

$x++;

}

$res = array_unique($result);

return $res;

}

Подпись: Пример:

$test = range(1, 15);

foreach (sort_array_rand($test) as $val) {

echo $val . '<br/>';

}

или

$test = range(1, 15);

$test = sort_array_rand($test);

echo '<pre>';

print_r($test);

[deleted] ( off )
( 08:05 21-02-2017 )
Генерация html таблицы

/* html_table.class.php */

class html_table

{



public $data = '';



/**

* Construct

*/



public function __construct($data = array(), $params = '')

{



if (! empty ($data)) {



$this->_generate($data);

$this->data = '<table' . (! empty($params) ? ' ' . $params : '') . '>' .

$this->data . '</table>';

}



}



/**

* Table generation

*/



private function _generate($data = array())

{

foreach ($data as $value) {



$this->data .= '<tr>';

foreach ($value as $param => $value_td) {

$this->data .= '<td' . (gettype($param) === 'integer' ? '' : ' ' . $param) . '>' . $value_td . '</td>';

}

$this->data .= '</tr>';



}

}



}

/* End of html_table.class.php */

?>

<?php

/* some_script.php */

require ('html_table.class.php');



$data = array(

array('style="color: red"' => 'item 1', 'item 2', 'item 3'),

array('value 1', 'value 2', 'value 3'),

array('value 01', 'value 02', 'value 03')

);



$table = new html_table($data, 'border="1"');



echo $table->data;

/* Enf of some_script.php */

[deleted] ( off )
( 07:04 25-02-2017 )
Время до 00:00

<?php



$curr=getdate();

$do=mktime(23, 59, 59, $curr['mon'], $curr['mday'], $curr['year'])-time();

echo'До конца дня осталось ' . round($do/3600, 0) . ' часов';



?>

[deleted] ( off )
( 07:05 25-02-2017 )
Правильный импорт файлов

<?php

$getfile = 'http://адрес.ру/myfile.zip';

$filename = 'сохранить_как.zip';

$data = file_get_contents($getfile);

$handle = fopen($filename,

"w");

fwrite($handle,$data);

fclose($handle);

?>

[deleted] ( off )
( 07:06 25-02-2017 )
Смещение временной зоны

<?



# @$zone - временная зона от -11 до +12

# @$def_zone - временная зона по умолчанию

# т.е. какую в. зону считать нулевой

# @$true - выполнять имитацию другой временной зоны

function world_time($zone, $def_zone = '+3', $true = true)

{

$time = $true ? time() + $def_zone * 3600 : time();

return date("d.m.Y H:i:s", $time + $zone * 3600 - $def_zone * 3600);

}

?>

[deleted] ( off )
( 21:51 01-03-2017 )
Рандомный вывод строки

$file = "wellcome.txt";

$fh = fopen($file, "r+") or die("Файл ($file) не существует");

$fa = file($file);

$an = array_rand($fa);

echo "$fa[$an]";

[deleted] ( off )
( 21:52 01-03-2017 )
Логирование обращений

<?php



$dir = "logs"; // папка для хранения логов //

$ip = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'] ;

$us = $_SERVER['HTTP_USER_AGENT'];

$date = date("d-m-Y");

$time = date("H:i:s");

$path = $_SERVER['REQUEST_URI'];

$ref = $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : 'none';



if(!is_dir($dir))

{

mkdir($dir,0777,TRUE);

}



$fp = fopen ("$dir/$date.txt", 'a');

fputs ($fp, "Date: $date | time: $time | IP: $ip | in: $path | out: $ref | UA: $us.n");

fclose ($fp);



?>

Далее »
« Назад
Страница: 1, 2, 3 ... , 18, 19, 20
[ Ответить ]
[Обновить]
[Добавить в закладки]
Мобильная связь и Интернет » Интернет-ресурсы » Полезные куски кодов [[deleted]]
На главную
© sasisa.org