手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表分类:PHP

关注着一些所关注的东西【加密了】

这篇日志被加密了,请输入密码后查看。

PHP获取当前网址/替换网址query中的值

这是一篇来自虫少侠的文章,事实上我也写过类似的。不过我当初是用在模版里,写在模版里为smarty中的。。。用法大致为{$url|urlreplace:"abc=def"},也就是把url里abc的键值换为Def,这样就避免在参数过多的时候,我一一输入了。。适合用在搜索后,一些Tag标签的切换等。。。。

下面就是虫少侠的内容了:

项目中用到的两个函数,自己写了下,放这做为备份吧。

一是PHP获取当前页面的网址:

PHP代码
  1. //获得当前的脚本网址【我稍整理了一下。。。鄙视不整理代码的人】  
  2. function GetCurUrl(){  
  3.     if(!emptyempty($_SERVER["REQUEST_URI"])){  
  4.         $scriptName = $_SERVER["REQUEST_URI"];  
  5.         $nowurl = $scriptName;  
  6.     }else{  
  7.         $scriptName = $_SERVER["PHP_SELF"];  
  8.         if(emptyempty($_SERVER["QUERY_STRING"])) $nowurl = $scriptName;  
  9.         else $nowurl = $scriptName."?".$_SERVER["QUERY_STRING"];  
  10.     }  
  11.     return $nowurl;  
  12. }  

另一个是PHP替换网址中query部分的某变量的值比如 ,我们要设$url中的key=321;

其实有几种情况:

$url=’www.sina.com/a.php?key=330′;

或$url=’www.sina.com/a.php;

或$url=’www.sina.com/a.php?cat=2′;

等等。虽然情况很多,但PHP处理起来非常简单,如下:

PHP代码
  1. /* 将URL中的某参数设为某值*/ //【这一段就挺好啊】  
  2. function url_set_value($url,$key,$value)  
  3. {  
  4.     $a=explode('?',$url);  
  5.     $url_f=$a[0];  
  6.     $query=$a[1];  
  7.     parse_str($query,$arr);  
  8.     $arr[$key]=$value;  
  9.     return $url_f.'?'.http_build_query($arr);  
  10. }  

原文来自:http://roln.cn/2009/07/30/lamp/some-php-url-function/

不过我的替换是这样写的。。当然也很烂 。。

PHP代码
  1. <?php  
  2. /** 
  3.  *使用例程:可用于分页类或页面中的替换等 
  4.     $url = "add_jd.php?pid=4&tb=gm_jd&page=1"; 
  5.     echo( "原始的URL:" . $url ); 
  6.     echo( '<br/>' ); 
  7.     echo( "字符串参数:" . url::replace( $url , "pid=10,page=2") ); 
  8.     echo( '<br/>' ); 
  9.     echo( "数组型参数:" . url::replace( $url , array('pid'=>10,'page'=>5)) ); 
  10.     //echo( urlReplace( $url , array('pid'=>10,'page'=>5)) ); 
  11. */  
  12. /** 
  13.  * url replace 
  14.  * @param string $url 需要替换的URL字符串,一般为aaa.php?abc=def,也可以带上路径,象http://xxx.com/abc/def.php?aa=bb 
  15.  * @param mixed $options 需要替换的变量,可以是字符串或数组,如果是字符串,格式为"aa=bb,cc=dd",有多个,用","隔开 
  16.  * @return string $url 替换后的URL 
  17.  */  
  18. class url   
  19. {  
  20.     static function replace ( $url , $options)  
  21.     {  
  22.         $options = self::optInit( $options );  
  23.         $Query = parse_url$url , PHP_URL_QUERY );  
  24.         if($Query){  
  25.             parse_str$Query , $GET );  
  26.             if ( $GET ){  
  27.                 //foreach ( $GET as $_k => $_v ){  
  28.                 //  //if( array_key_exists( $_k , $options)){  
  29.                 //      $GET[$_k] = $options[$_k];  
  30.                 //  //}  
  31.                 //}  
  32.                 $GET = array_merge($GET,$options);  
  33.             }  
  34.             return str_replace$Query , http_build_query( $GET ), $url );            
  35.         }  
  36.         if( !$Query && $options ){  
  37.             return $url . "?" . http_build_query($options);  
  38.         }  
  39.         return $url;  
  40.     }  
  41.   
  42.     static private function optInit ( $options )  
  43.     {  
  44.         ifis_string$options )){  
  45.             $optlists = Power::Normalize( $options );  
  46.             foreach$optlists as $val){  
  47.                 list($tmpKey,$tmpVal) = Power::Normalize( $val , "=");  
  48.                 $opts[$tmpKey] = $tmpVal;  
  49.             }  
  50.         }else{  
  51.             $opts = $options;  
  52.         }  
  53.         //unset( $options );  
  54.         return $opts;  
  55.     }  
  56. }  
......虽然考虑了一些东西,但也仅仅是个很一般的解决方法

 

Tags: parse_str, parse_url

框架开发一点小注意事项

一般来说,FORM我们都是用POST方式提交的,但也有需要GET方式提交的表单.这种情况出现最多的就应该是Search了。。
如果没有采用框架,那么我们的搜索POST的action就可能是 search.php了。而如果采用框架,并且是单入口的框架,这样的Action地址往往可能是index.php?module=search&action=titlesearch之类。但如果你真的这样把这个URL放到action里面提交,你会发现,你所有的参数都扔到index.php?module=index&action=index这样的页面里去了。

这是为什么呢?其实就是因为form中,如果method是GET方式,它是不接受url后面带参数的,而是把form里的元素自己组成get参数进行传递。所以,如果需要用到get方式,请手动在表单中使用隐藏域把module和Action等赋值进去。

但凡是也有例外,单入口的PHP,如果使用pathinfo模式,好象就能 够规避这种问题,比如我的URL是index.php/module/search/action/titlesearch/这时候,框架就能够被正常接收了。原理嘛。。。。黑黑,自己想想喽。

其实我这篇勃客只是做个笔记而己,因为在做thinksns第三方开发的时候,想使用get方式做查询的时候遇到了这个问题,而thinksns的url_mode用的是兼容模式,所以就出现了这种情况。事后已经向流年提起,流年好象是说过在thinkphp的框架中想法改进一下url_mode=3,兼容模式下存在的这种问题。

或许,现在的SVN上,它已经改掉了吧?可惜我用的thinksns不能随便更新核 心,真痛苦。

Tags: thinksns, thinkphp

thinksns部分更新记录【自己更改的】

由于自己更改了thinksns的部分核心内容,不得己,先记录下来,如果以后被thinksns官方所更改,那么,我就从其中去掉,否则。。。每次TS一更新,我都要重新更新这些地方

1、关于Ucenter的整合【主要是添加了API等,还有就是用户登录和退出那块,暂时不更换】

2、修改了/thinkphp/api/Model/AttachLWModel.class.php的upload方法【约 line:69】,在foreach($uploadinfo as $u)这个循环中,添加了

PHP代码
  1. $map['key'] = $u['key'];  

主要原因是,这个api只能针对页面中的FILE上传表单处理,而且,都是同名字段的表单,这对我来说有点不方便,比如我的表单里有<input type="ffile" name="a"><input type="file" name="b">,这个API就不能工作,所以添加了这个变量处理: $u['key']这个键值在thinkPHP的upload里是存在的,所以直接添加就可以了。

 

而且这个变量的赋值,相信在以后的thinksns上传附件有多个不同字段的时候,应该是会用得到的。。。

 

 

 

Tags: thinksns, upload

Using PHP As A Shell Scripting Language

这是一篇有点老的文章了,但是因为它带有译文,所以我就转摘一下。
命令行下的PHP,以前也是经常用的,现在用的少了。毕竟命令下行,效果更好,以前都是用来循环处理订单和检测无效信息的。。。
前两天我写了博客说bluehost的命令行不支持yii框架呢。。

以下是原文:

Author:Darrell Brogdon

As most of us already know, PHP is the best language for developing dynamic web pages available today. Not many people are aware that it can be used as a shell scripting language as well. While PHP as a shell script isn't as robust as Bash or Perl it does have definite advantages, especially if you're like me and are more proficient in PHP than you are in Perl.
The requirements for using PHP as a shell language is that you must compile PHP as a CGI binary instead of as an Apache module. There are certain security issues related to this so please refer to the PHP Manual when doing so.
Where the code is concerned, the only difference between a PHP shell script and a regular PHP web page is the existence of the standard shell call at the top of the script:
#!/usr/local/bin/php -q
We're using the '-q' switch so that the HTTP headers are suppressed. Also, you're still required to begin and end the script with the standard PHP tags:
<?php ?>
So let's delve into the standard code sample we all know and love:
#!/usr/local/bin/php -q
<?php

   
print("Hello, world!\n"
);

?>

This, as most of you know already (about a billion times over) will simply output to the screen "Hello, world!".

Passing arguments to the shell script

Commonly with a shell script you need to pass arguments to the script. This is easily done using the built-in '$argv' array as show in the following example:
#!/usr/local/bin/php -q
<?php
    $first_name
= $argv[1
];
   
$last_name = $argv[2
];

    print(
"Hello, $first_name $last_name!  How are you today?\n"
);

?>

So in the above script we're printing out the first two arguments to the script which would be called like this:
[dbrogdon@artemis dbrogdon]$ scriptname.ph Darrell Brogdon
Which would print out:
Hello, Darrell Brogdon!  How are you today?
[dbrogdon@artemis dbrogdon]$


The only major difference with the '$argv' array between a shell script and a web page is that in a shell script, '$argv[0]' is the name of your script. In a web page it is the first argument in your query string.
Making a script more interactive
But how do we wait for user input? How do we create a truly interactive script? Well, PHP has no native functions like the 'read' command in shell but we can always emulate it using the following PHP function:
*Note that this function will only work for Unix.

<?php

function read
() {
   
$fp=fopen("/dev/stdin", "r"
);
   
$input=fgets($fp, 255
);
   
fclose($fp
);

    return
$input
;
}

?>

This function opens a file pointer to Standard In (/dev/stdin on Linux) and reads anything from this pointer up to 255 bytes, newline, or EOF. In this case a newline is most likely to occur. It then closes the file pointer and returns the data.

So now let's modify our previous script to wait for user input using the newly created 'read()' function:
#!/usr/local/bin/php -q
<?php

   
function read
() {
        
$fp=fopen("/dev/stdin", "r"
);
        
$input=fgets($fp, 255
);
        
fclose($fp
);

        return
$input
;
    }

    print(
"What is your first name? "
);
   
$first_name = read
();

    print(
"What is your last name? "
);
   
$last_name = read
();

    print(
"\nHello, $first_name $last_name!  Nice to meet you!\n"
);

?>

You may notice, however, that when you execute this script the last line to be printed is broken into three lines instead of one as it should be. This is because our 'read()' function also takes in the newline character. This is easily fixed by stripping off the newline before we return the data:

<?php

function read
() {
   
$fp=fopen("/dev/stdin", "r"
);
   
$input=fgets($fp, 255
);
   
fclose($fp
);

    return
str_replace("\n", "", $input
);
}

?>
Embedding PHP shell scripts within a regular shell script
Sometimes it might be handy to embed a PHP shell script within a script written in Bash or other shell. This is fairly simple but can get a tad tricky.
First, how to embed the PHP code:
#!/bin/bash
echo This is the Bash section of the code.

/usr/local/bin/php -q << EOF
<?php
   
print("This is the PHP section of the code\n"
);
?>
EOF

Pretty simple huh? Until you add a variable that is. This is the tricky part. Try running the following code segment:
#!/bin/bash
echo This is the Bash section of the code.

/usr/local/bin/php -q << EOF
<?php
    $myVar
= "PHP"
;
    print(
"This is the $myVar section of the code.\n"
);
?>
EOF

You'll get the following error:
<b>Parse error</b>: parse error in <b>-</b> on line <b>2</b><br>
To fix this you have to escape all of the '$' characters in your PHP code:
#!/bin/bashecho This is the Bash section of the code./usr/local/bin/php -q << EOF<?php        \$myVar = "PHP";        print("This is the \$myVar section of the code.\n");?>EOF
So that should get you started on creating your own shell scripts using PHP!


译文:
可能很多人都想过使用PHP编写一些定时发信之类的程序,但是却没有办法定时执行PHP;一次去PHPBuilder的时候,发现了这一篇文章,于是想给大家翻译一下(同时做了一些修改),希望对大家有用。第一次翻译文章,不好请多多见谅。

我们都知道,PHP是一种非常好的动态网页开发语言(速度飞快,开发周期短……)。但是只有很少数的人意识到PHP也可以很好的作为编写Shell脚本的 语言,当PHP作为编写Shell脚本的语言时,他并没有Perl或者Bash那么强大,但是他却有着很好的优势,特别是对于我这种熟悉PHP但是不怎么 熟悉Perl的人。
要使用PHP作为Shell脚本语言,你必须将PHP作为二进制的CGI编译,而不是Apache模式;编译成为二进制CGI模式运行的PHP有一些安全性的问题,关于解决的方法可以参见PHP手册(http://www.php.net)。
一开始你可能会对于编写Shell脚本感到不适应,但是会慢慢好起来的:将PHP作为一般的动态网页编写语言和作为Shell脚本语言的唯一不同就在于一个Shell脚本需要在第一行生命解释本脚本的程序路径:
#!/usr/local/bin/php -q
我们在PHP执行文件后面加入了参数“-1”,这样子PHP就不会输出HTTPHeader(如果仍需要作为Web的动态网页,那么你需要自己使用header函数输出HTTPHeader)。当然,在Shell脚本的里面你还是需要使用PHP的开始和结束标记:
<?php 代码 ?>
现在让我们看一个例子,以便于更好的了解用PHP作为Shell脚本语言的使用:
#!/usr/local/bin/php -q
<?php
print("Hello, world!\n");
?>
上面这个程序会简单的输出“Hello, world!”到显示器上。

一、传递Shell脚本运行参数给PHP:
作为一个Shell脚本,经常会在运行程序时候加入一些参数,PHP作为Shell脚本时有一个内嵌的数组“$argv”,使用“$argv”数组可以很 方便的读取Shell脚本运行时候的参数(“$argv[1]”对应的是第一个参数,“$argv[2]”对应的是第二个参数,依此类推)。比如下面这个 程序:
#!/usr/local/bin/php -q
<?php
$first_name = $argv[1];
$last_name = $argv[2];
printf("Hello, %s %s! How are you today?\n", $first_name, $last_name);
?>
上面的代码在运行的时候需要两个参数,分别是姓和名,比如这样子运行:
[dbrogdon@artemis dbrogdon]$ scriptname.ph Darrell Brogdon
Shell脚本在显示器上面会输出:
Hello, Darrell Brogdon! How are you today?
[dbrogdon@artemis dbrogdon]$
在PHP作为动态网页编写语言的时候也含有“$argv”这个数组,不过和这里有一些不同:当PHP作为Shell脚本语言的时候“$argv[0]”对 应的是脚本的文件名,而当用于动态网页编写的时候,“$argv[1]”对应的是QueryString的第一个参数。

二、编写一个具有交互式的Shell脚本:
如果一个Shell脚本仅仅是自己运行,失去了交互性,那么也没有什么意思了。当PHP用于Shell脚本的编写的时候,怎么读取用户输入的信息呢?很不 幸的是PHP自身没有读取用户输入信息的函数或者方法,但是我们可以效仿其他语言编写一个读取用户输入信息的函数“read”:
<?php
function read() {
$fp = fopen('/dev/stdin', 'r');
$input = fgets($fp, 255);
fclose($fp);
return $input;
}
?>
需要注意的是上面这个函数只能用于Unix系统(其他系统需要作相应的改变)。上面的函数会打开一个文件指针,然后读取一个不超过255字节的行(就是fgets的作用),然后会关闭文件指针,返回读取的信息。
现在我们可以使用函数“read”将我们前面编写的程序1修改一下,使他更加具有“交互性”了:
#!/usr/local/bin/php -q
<?php
function read() {
$fp = fopen('/dev/stdin', 'r');
$input = fgets($fp, 255);
fclose($fp);
return $input;
}
print("What is your first name? ");
$first_name = read();
print("What is your last name? ");
$last_name = read();
print("\nHello, $first_name $last_name! Nice to meet you!\n");
?>
将上面的程序保存下来,运行一下,你可能会看到一件预料之外的事情:最后一行的输入变成了三行!这是因为“read”函数返回的信息还包括了用户每一行的结尾换行符“\n”,保留到了姓和名中,要去掉结尾的换行符,需要把“read”函数修改一下:
<?php
function read() {
$fp = fopen('/dev/stdin', 'r');
$input = fgets($fp, 255);
fclose($fp);
$input = chop($input); // 去除尾部空白
return $input;
}
?>

三、在其他语言编写的Shell脚本中包含PHP编写的Shell脚本:
有时候我们可能需要在其他语言编写的Shell脚本中包含PHP编写的Shell脚本。其实非常简单,下面是一个简单的例子:
#!/bin/bash
echo This is the Bash section of the code.

/usr/local/bin/php -q << EOF
<?php
print("This is the PHP section of the code\n");
?>
EOF
其实就是调用PHP来解析下面的代码,然后输出;那么,再试试下面的代码:
#!/bin/bash
echo This is the Bash section of the code.

/usr/local/bin/php -q << EOF
<?php
$myVar = 'PHP';
print("This is the $myVar section of the code\n");
?>
EOF
可以看出两次的代码唯一的不同就是第二次使用了一个变量“$myVar”,试试运行,PHP竟然给出出错的信息:“Parse error: parse error in - on line 2”!这是因为Bash中的变量也是“$myVar”,而Bash解析器先将变量给替换掉了,要想解决这个问题,你需要在每个PHP的变量前面加上“\” 转义符,那么刚才的代码修改如下:
#!/bin/bash
echo This is the Bash section of the code.

/usr/local/bin/php -q << EOF
<?php
\$myVar = 'PHP';
print("This is the \$myVar section of the code\n");
?>
EOF

好了,现在你可以用PHP编写你自己的Shell脚本了,希望你一切顺利。如果有什么问题,可以去http://www.PHPBuilder.com或者http://www.zPHP.com上面讨论。

---EOF---

仔细看上面的一段, fopen("/dev/stdin"),如果你看过yii的框架,你会发现,现在很多人都在使用php://stdin,其实,这只是PHP把这种封装成了一个协议,更方便而己。。可以参考yiiframework.com