Generate a phrase to url.

str_replace
(PHP 4, PHP 5)

str_replace — Replace all occurrences of the search string with the replacement string

$vowels = array(“a”, “e”, “i”, “o”, “u”, “A”, “E”, “I”, “O”, “U”);
$onlyconsonants = str_replace($vowels, “”, “Hello World of PHP”);

Posted in Php Code, Php Functions. Comments Off on Generate a phrase to url. »

How to write IE specific PHP

if((ereg("Nav", getenv("HTTP_USER_AGENT"))) || (ereg("Gold", getenv("HTTP_USER_AGENT"))) || (ereg("X11", getenv("HTTP_USER_AGENT"))) || (ereg("Mozilla", getenv("HTTP_USER_AGENT"))) || (ereg("Netscape", getenv("HTTP_USER_AGENT"))) AND (!ereg("MSIE", getenv("HTTP_USER_AGENT")) AND (!ereg("Konqueror", getenv("HTTP_USER_AGENT"))))) $browser = "Netscape";
elseif(ereg("MSIE", getenv("HTTP_USER_AGENT"))) $browser = "MSIE";
elseif(ereg("Lynx", getenv("HTTP_USER_AGENT"))) $browser = "Lynx";
elseif(ereg("Opera", getenv("HTTP_USER_AGENT"))) $browser = "Opera";
elseif(ereg("WebTV", getenv("HTTP_USER_AGENT"))) $browser = "WebTV";
elseif(ereg("Konqueror", getenv("HTTP_USER_AGENT"))) $browser = "Konqueror";
elseif((eregi("bot", getenv("HTTP_USER_AGENT"))) || (ereg("Google", getenv("HTTP_USER_AGENT"))) || (ereg("Slurp", getenv("HTTP_USER_AGENT"))) || (ereg("Scooter", getenv("HTTP_USER_AGENT"))) || (eregi("Spider", getenv("HTTP_USER_AGENT"))) || (eregi("Infoseek", getenv("HTTP_USER_AGENT")))) $browser = "Bot";
else $browser = "Other";

this will return the browser name in $browser

and you can have a test at http://www.botsvsbrowsers.com/SimulateUserAgent.asp.

example:

if(ereg(“Google”, getenv(“HTTP_USER_AGENT”))) $browser = “Bot”;
else $browser = “Other”; 

echo $browser;

Posted in Php Code. Comments Off on How to write IE specific PHP »

Plugin for limit the size of the post in the main page in WordPress

The  plugin  for WordPress in order to control the maximum amount of characters displayed for an entry on the main page. If the set limit is surpassed a link to a page with the whole content will appear and the text on the entry will be chopped to that amount of characters, otherwise the content will be showed unchanged. (more…)

Posted in Php Code, WordPress. Comments Off on Plugin for limit the size of the post in the main page in WordPress »

How to add ads only at the first post?

It is very simple:

<?php if ($wp_query->current_post == 0) : ?>

<div style=”float:right”>

ads come here.

</div>
<?php endif; ?>

If you don’t want the ads show at homepage, using:

<?php if (!is_home()) ?>

Posted in Php Code, WordPress. Comments Off on How to add ads only at the first post? »

Sending Email (Text/HTML/Attachments)

Email is the most popular Internet service today. A plenty of emails are sent and delivered each day. The goal of this tutorial is to demonstrate how to generate and send emails in PHP.

So, you want to send automated email messages from your PHP application. This can be in direct response to a user’s action, such as signing up for your site, or a recurring event at a set time, such as a monthly newsletter. Sometimes email contains file attachments, both plain text and HTML portions, and so on. To understand how to send each variation that may exist on an email, we will start with the simple example and move to the more complicated.

Sending a Simple Text Email
Sending HTML Email
Sending Email with Attachments
Note that to send email with PHP you need a working email server that you have permission to use: for Unix machines, this is often Sendmail; for Windows machines, you must set the SMTP directive in your php.ini file to point to your email server.

Sending a Simple Text Email (more…)

Posted in Php Code. Comments Off on Sending Email (Text/HTML/Attachments) »

How to Get the Current Page URL

Sometimes, you might want to get the current page URL that is shown in the browser URL window. For example if you want to let your visitors submit a blog post to Digg you need to get that same exact URL. There are plenty of other reasons as well. Here is how you can do that. (more…)

Posted in Php Code, Php Skills, Php Study. Comments Off on How to Get the Current Page URL »

How to usp php to ban ip

First build a database table:

CREATE TABLE `su_lockip` (
  `id` int(4) NOT NULL auto_increment,
  `lockip` varchar(1024) default NULL,
  PRIMARY KEY  (`id`)
) 

Second, design a page so that you can add the ip you want to ban, every ip is devided by |,  I only write the mail part:
 

$UlockIp=$_POST[‘z’]?$_POST[‘z’]:”;
 if(empty($UlockIp)){
  exit(“<script>alert(‘Sorry, the message your input is wrong!’);history.back();</script>”);
 }
 $sql=”update su_lockip set lockip=’$UlockIp'”;
 if(mysql_query($sql)){
  exit(“<script>alert(‘Locked succesfully!’);history.back();</script>”);
 }else{
  exit(“<script>alert(‘Sorry, the message your input is wrong!’);history.back();</script>”);
 } 

Last, check the ip is in the database or not, if in the database, then show killed.

function lock_user_ip(){
 $Usql =mysql_query(“select * from su_lockip”);
 $Urs =mysql_fetch_array($Usql);
 $UlockIp=$Urs[‘lockip’];
 $ClockIp=$this->get_real_ip();
 $Iplist =explode(‘|’,$UlockIp);
 if(in_array($ClockIp,$Iplist)){
  exit(‘sorry system lock your IP’);
 }
 }
 
 function get_real_ip(){
   $ip=false;
   if(!empty($_SERVER[“HTTP_CLIENT_IP”])){
    $ip = $_SERVER[“HTTP_CLIENT_IP”];
   }
   if (!empty($_SERVER[‘HTTP_X_FORWARDED_FOR’])) {
    $ips = explode (“, “, $_SERVER[‘HTTP_X_FORWARDED_FOR’]);
    if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
    for ($i = 0; $i < count($ips); $i++) {
     if (!eregi (“^(10|172\.16|192\.168)\.”, $ips[$i])) {
      $ip = $ips[$i];
      break;
     }
    }
   }
   return ($ip ? $ip : $_SERVER[‘REMOTE_ADDR’]);
 } 

Posted in Php Code, Php Study. Comments Off on How to usp php to ban ip »

List 50 posts of wordpress.

If you want to list 50 posts, use the following code:

<ul>
 <?php
 global $post;
 $myposts = get_posts(‘numberposts=-1’);
 foreach($myposts as $post) :
 ?>
    <li><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></li>
 <?php endforeach; ?>
 </ul>

if you want to list all the posts, use the following codes:

<ul>
 <?php
 global $post;
 $myposts = get_posts(‘numberposts=5’);
 foreach($myposts as $post) :
 ?>
    <li><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></li>
 <?php endforeach; ?>
 </ul>

http://codex.wordpress.org/Template_Tags/get_posts

Posted in Php Code, WordPress. Comments Off on List 50 posts of wordpress. »

How to get root dirctory by php

<?php echo realpath(‘./’)?>

very simple, right?

Posted in Php Code. Comments Off on How to get root dirctory by php »

Retrieve a Particular Post or Page

Retrieve a Particular Post
<?php
// retrieve one post with an ID of 5
query_posts(‘p=5’);     
?>
If you want to use the Read More functionality with this query, you will need to set the global $more variable to 0.

<?php
// retrieve one post with an ID of 5
query_posts(‘p=5’);
     
global $more;
// set $more to 0 in order to only get the first part of the post
$more = 0;

// the Loop
while (have_posts()) : the_post();
  // the content of the post
  the_content(‘Read the full post »’);
endwhile;
?>
Retrieve a Particular Page
To retrieve a particular page, you could use the following:

<?php
query_posts(‘page_id=7’);      //retrieves page 7 only
?>

or

<?php
query_posts(‘pagename=about’); //retrieves the about page only
?>

Posted in Php Code, WordPress. Comments Off on Retrieve a Particular Post or Page »
King Php Science Welcome, you are from: 54.224.76.106