php 创建短地址 (var_export)
提交页面 shorten.php:
?
?
??
处理页面 show.php:
?
?
<?php include 'functions.php'; include 'shorturls.php'; $entry = $_POST['submit']; $url = $_POST['url']; $output = ''; if($entry == 'Shorten Url' AND $url) { if(!in_array($url,$shortUrls)) { $uif = alphaID(count($shortUrls),false,6); if(!array_key_exists($uif,$shortUrls)) { $shortUrls[$uif] = $url; $file = fopen('shorturls.php','w'); fwrite($file,'<?php $shortUrls = ' . var_export($shortUrls,true) . ';?>'); fclose($file); $output = 'Original Url : '.$url.'<br> Shorten Ulr : <a href="http://www.yourdomain.com/api/s='.%24uif.'" target="_blank">http://www.yourdomain.com/api/s='.$uif.'</a>'; } else $output = 'access error'; } else { $uif = array_search($url,$shortUrls); $output = 'Original Url : '.$url.'<br> Shorten Ulr : <a href="http://www.yourdomain.com/api/s='.%24uif.'" target="_blank">http://www.yourdomain.com/api/s='.$uif.'</a>'; } echo $output.'<br><br> <a href="shorten.php">Back</a>'; } ?>?
?
关联页面?functions.php
?
?
<?php function alphaID($in, $to_num = false, $pad_up = false, $passKey = null) { $index = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; if ($passKey !== null) { // Although this function's purpose is to just make the // ID short - and not so much secure, // with this patch by Simon Franz (http://blog.snaky.org/) // you can optionally supply a password to make it harder // to calculate the corresponding numeric ID for ($n = 0; $n<strlen($index); $n++) $i[] = substr( $index,$n ,1); $passhash = hash('sha256',$passKey); $passhash = (strlen($passhash) < strlen($index)) ? hash('sha512',$passKey) : $passhash; for ($n=0; $n < strlen($index); $n++) $p[] = substr($passhash, $n ,1); array_multisort($p, SORT_DESC, $i); $index = implode($i); } $base = strlen($index); if ($to_num) { // Digital number <<-- alphabet letter code $in = strrev($in); $out = 0; $len = strlen($in) - 1; for ($t = 0; $t <= $len; $t++) { $bcpow = bcpow($base, $len - $t); $out = $out + strpos($index, substr($in, $t, 1)) * $bcpow; } if (is_numeric($pad_up)) { $pad_up--; if ($pad_up > 0) $out -= pow($base, $pad_up); } $out = sprintf('%F', $out); $out = substr($out, 0, strpos($out, '.')); } else { // Digital number -->> alphabet letter code if (is_numeric($pad_up)) { $pad_up--; if ($pad_up > 0) $in += pow($base, $pad_up); } $out = ""; for ($t = floor(log($in, $base)); $t >= 0; $t--) { $bcp = bcpow($base, $t); $a = floor($in / $bcp) % $base; $out = $out . substr($index, $a, 1); $in = $in - ($a * $bcp); } $out = strrev($out); // reverse } return $out; } ?>?
?
关联页面?shorturls.php
?
?
<?php $shortUrls = array ();?>?
?
?
原理: 将提交的网址存放数组里面,同时为之产生一个唯一的键,之后将该数组存放文本中,当然你也可以存放到数据库中,此实例为存放到文本中。
?
一些函数的解释:
?
1. array_search() 函数
?
array_search(value,array,strict)
?
array_search() 函数与 in_array() 一样,在数组中查找一个键值。如果找到了该值,匹配元素的键名会被返回。如果没找到,则返回 false。
?
2.?var_export()?函数
?
?
此函数返回关于传递给该函数的变量的结构信息,它和 var_dump() 类似,不同的是其返回的表示是合法的 PHP 代码。
您可以通过将函数的第二个参数设置为 TRUE,从而返回变量的表示。
?
比较 var_export() 和 var_dump().
?
?
?
最后是 接受页面?redirect.php
?
?
<?php include 'shorturls.php'; $uid = $_GET['uid']; $addr = ($uid AND array_key_exists($uid,$shortUrls)) ? $shortUrls[$uid]: '/'; header('location:'.$addr);?
此页面需要结合 .htaccess or .waccess 使用
?
?
RewriteEngine On RewriteRule ^s=(.*)$ /redirect.php?uid=$1 [R=301,L]?
?
?
?
关联:
php创建短ID Create short IDs with PHP - Like Youtube or TinyURL ?
?
?
如果问题,敬请留言~
?
?

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SecList
SecLists는 최고의 보안 테스터의 동반자입니다. 보안 평가 시 자주 사용되는 다양한 유형의 목록을 한 곳에 모아 놓은 것입니다. SecLists는 보안 테스터에게 필요할 수 있는 모든 목록을 편리하게 제공하여 보안 테스트를 더욱 효율적이고 생산적으로 만드는 데 도움이 됩니다. 목록 유형에는 사용자 이름, 비밀번호, URL, 퍼징 페이로드, 민감한 데이터 패턴, 웹 셸 등이 포함됩니다. 테스터는 이 저장소를 새로운 테스트 시스템으로 간단히 가져올 수 있으며 필요한 모든 유형의 목록에 액세스할 수 있습니다.

ZendStudio 13.5.1 맥
강력한 PHP 통합 개발 환경

에디트플러스 중국어 크랙 버전
작은 크기, 구문 강조, 코드 프롬프트 기능을 지원하지 않음
