php短网址超简单代码

巴扎黑
发布: 2016-12-07 14:15:55
原创
2100人浏览过

php短网址超简单代码 
系统环境: 
php,apache2,linux 

生成php短网址的操作: 
把代码复制到index.php放在一个只有1个字符(如u)作为文件夹名的二级目录中。 
为此目录增加写权限,图省事就chmod 777 u (根目录也行,为避免影响别的文件可能要改改代码) 

网址生成结果: 
把http://www.heimaolianmeng.com/heimaoseojishu/变成http://127.0.0.1/u/1 
php生成短网址的原理: 
1,通过form post获取要变短的url 
2,把url放在一个javascript内写入文件,文件名按数字增长。javascript的作用就是跳转到指定的url 

可优化: 
如果可以设置二级域名,那就把二级域名指向那个目录就好了,就不用多输入一个 u/。 

代码: 


代码示例: 
<html> 
<head> 
<meta charset="utf-8" /> 
<title>shorten url</title> 
</head> 
<body> 
url to be shortened: (must include protocol like http:// or https:// etc.)<br /> 
<form method="post"> 
<textarea rows='3' name="url" ></textarea><br /> 
<input type="submit" value="submit" /> 
<form><br /> 
<?php 
if (isset($_post['url'])) { 
$origin = $_post['url']; 
if (strlen($origin) > 10) { 
$filename = count(scandir('.')) - 3; // strip php self . .. 
file_put_contents($filename, 
'<script type="text/javascript">location.href="'.$origin.'"</script>'); 
$shortened = "http://".$_server['http_host'].dirname($_server['php_self']).'/'.$filename; 
echo 'original url is<br /><a href="'.$origin.'">'.$origin.'</a><br />' 
.'shortened url is<br /><a href="'.$shortened.'">'.$shortened.'</a>'; 
} else { 
echo "the url you entered is not valid."; 


?> 
</body> 
</html> 
生成以数字为文件名的文件: 


代码示例: 
<script type="text/javascript">location.href="http://www.heimaolianmeng.com/heimaoseojishu/"</script>

知网AI智能写作
知网AI智能写作

知网AI智能写作,写文档、写报告如此简单

知网AI智能写作 38
查看详情 知网AI智能写作
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号