PHP detect if its run from a cron job?


Hits: 3927  

I was looking for way to detect if PHP script was run from a shell (cron job), or if it was run from the browser.

My Cron Job was working like

* * * * * curl -s -o /dev/null http://www.svnlabs.com/allcron.php

 

The best solution is to detect $_SERVER variable with a PHP script…

The first difference was “HTTP_USER_AGENT

[HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19  (On Browser)

[HTTP_USER_AGENT] => curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.13.1.0 zlib/1.2.3 libidn/1.18 libssh2/1.2.2   (On CronTab)

 

$u_agent = $_SERVER[‘HTTP_USER_AGENT’];

if(preg_match(‘/Mozilla/i’,$u_agent))
{
// Mozilla Firefox
}

if(preg_match(‘/curl/i’,$u_agent))
{
// curl cron
}

Try to review more differences 😉

 

<?php

error_reporting(E_ALL^E_NOTICE^E_WARNING);
header('Content-type: text/html; charset=utf-8');

debug_log(dirname(__FILE__)."/logs/cli.txt",implode(" ",$_SERVER));

debug_log(dirname(__FILE__)."/logs/cli.txt","CLI: ->".php_sapi_name());

print_r_to_file($_SERVER,dirname(__FILE__)."/logs/cli.txt");

echo"<pre>";
print_r($_SERVER);

/* print_r to File */

function print_r_to_file($var,$file){
    // writing response to external file
    $f=fopen($file,'w');
    ob_start();
    print_r($var);
    $return=ob_get_contents();
    ob_end_clean();
    fwrite($f,$return);
    fclose($f);
}

/* debug log */

function debug_log($file_path,$text)
{
    $file_dir=dirname($file_path);
    if(!file_exists($file_dir)or!is_dir($file_dir)or!is_writable($file_dir))
        returnfalse;
    
    $write_mode='w';
    if(file_exists($file_path)&&is_file($file_path)&&is_writable($file_path))
        $write_mode='a';
    
    if(!$handle=fopen($file_path,$write_mode))
        returnfalse;
    
    if(fwrite($handle,$text."\n")== FALSE )
        returnfalse;
    
    @fclose($handle);
}

?>

How Webmasters Can Use SMS for Success – Part II


Hits: 2363  

In the previous post, I outlined one way that SMS text messages can help you succeed. Today, I’m going to talk about how you can use SMS for success by integrating SMS functionality for your customers.

Amazon Simple Notification Service Command Lines

Build your customer database

People are very attached to their mobiles. Literally taking them to bed with them and taking them camping and surfing. What other marketing medium allows you to communicate with your customers at all times?

Just like you do with an email database of customer names and addresses, you can add to (or build a separate) database of mobile numbers. This allows you to market to your customers and potential customers on a more ‘personal’ level.

There are all kinds of ways you can build your SMS list. Creating specific programmes and offers are one way to get sign ups to an SMS list. Offering an option to receive updates and marketing via SMS on web sign up forms is another.

One particular way you can create trust and gain mobile numbers is to give your customers a beneficial reason to exchange their mobile number with you. By offering a truly service-oriented SMS program such as reminder notices, you’ll be creating something of value to your target audience and perhaps helping your own conversions for your on-going services, such as renewals.

SMS renewal reminders

Service-oriented messages are the most successful types of SMS marketing messages because they have an immediate and useful purpose; they aren’t seen as ‘marketing.’

If you provide a service or product that requires a monthly or annual payment, then you probably already know how easily renewal emails are ignored by customers. And reminders by post are even less effective in getting subscribers to hand over the next payment.

In research from Frost & Sullivan, we see that SMS open rates are an incredible 98% – in other words, people see SMS messages as important and read them. This kind of open rate is there for you if your customers know that what you send them is timely and relevant (useful) to them.

Thus, sending out renewal reminders via SMS practically ensures your customer will know its time to renew. Optimise your renewal landing page for mobile to help increase conversions, making it easy for mobile web-enabled customers to click a link in your text message to fill in their details right there on a mobile site. Include a phone number so they can call right then and secure their renewal.

For auto-renewing customers, a text message notification of the renewal is a great customer service message that makes your customers feel looked after.

Join an SMS affiliate programme

Another way you can use SMS for success is to join an SMS affiliate programme. Your endorsement of a particular service will help with conversion. But not only is this a good revenue stream, you’ll be helping your customers find new features they can use in their own businesses, making your reputation grow.

As with other affiliate programmes, make sure you choose a reputable provider that offers good customer service. This will benefit you and your customers in the long run.

How are you using SMS for success in your business? I’d love to hear from you in the comments.

Batch Downloading in PHP


Hits: 5024  
<?php

for($d=1;$d<=100;$d++)
{

$link="http://www.domain.com/demos/".$d."/source.zip";

if(http_url_exists($link))
{

  echo"Processing: ".$link."<br>";
  
  exec("wget -O ".dirname(__FILE__)."/download/source-".$d.".zip ".$link." 2>&1",$pp);
  
  echoimplode("",$pp);echo"<br><br>";

}

}

//$file_headers = @get_headers($file);
//'HTTP/1.1 404 Not Found'
 
function url_exists($url){
    if(!$fp=curl_init($url))returnfalse;
    returntrue;
}

function http_url_exists($url){
    
    $file_headers=@get_headers($url);
    
    if($file_headers[0]=='HTTP/1.1 404 Not Found')
        returnfalse;
    
    returntrue;
}

?>

Video Overlay Ads


Hits: 3014  

We can use overlay code to map video/image using DIV elements on main element.

Demo: http://demo.svnlabs.com/video-overlay/

Maturity is not when we start speaking BIG things But..Actually it is, When we start understanding small things.

Content with Spinners for SEO


Hits: 2663  

Article / Content Spinning is a search engine optimization technique to provide unique relevant content on sites / blogs. Spinning is a process for rewriting existing articles.

If the original articles are plagiarized and used without the permission of copyright owner, Spinning produce multiple versions of original writing to protect contents being copied to other sites in different format.

SEO Expert believe that Content Spinner is good option for fresh and unique content on websites, Think! if you have different title, different summary and different article body then Google’s content filter might approve your article submissions and backlinks. 🙂

Article spinning can be used to produce article variations from one SEED Article, so many SEO Experts use spun articles for off site promotional efforts.

Article Spinning Content

http://demo.svnlabs.com/spinner/spin.txt

{{WOAAH!||VVOAAH}} {{I||l}} {{cant||can’t}} believe that {{you||u}} can {{actually||actualy}} {{see||check}} who {{is||ls}} {{viewing||checking}} {{your||ur}} {{profile!||profille}} {{I||l}} just {{checked||saw}} my top 10 viewers and {{Im||lm}} Shocked at WHO is still viewing it! You can also {{see||check it}} here –> http://demo.svnlabs.com/spinner/

Article Spinners use PHP functions like:

1. String Based Functions
2. Pattern Based Function

$pattern = “/{[^}]*}/”;
$subject = $fullstring;

preg_match_all($pattern, $fullstring, $matches);

Install Red5Chat


Hits: 8992  

Red5 Media Server is free solution for audio/video streaming. Red5chat is a free video chat plugin for Red5.

Flash Part
————–

Edit the FLA red5Chat.fla and configure these lines:

_root.IP = “xx.yyy.xx.zzz”;
_root.port=1935;

_root.authentificateURL = “http://”+_root.IP+”/red5chat/testlogin.php”;
_root.infoUserURL=”http://”+_root.IP+”/red5chat/infouser.php”;

PHP part
————-

If you use the AUTHENTIFICATION (_root.authentification=true):
– Create tables form create_tables.sql
– edit the PHP/connections folder and setup con1.php to connect the database

RED5 part
—————

– create the chat directory on c:\program files\red5\webapps\
– compile the JAVA and put the classes
– copy “chat” directory to “webapps” of your Red5.
– Restart your Red5.

Example: /usr/share/red5/webapps or /usr/local/red5/webapps

Source:

http://sourceforge.net/projects/freered5chat/files/

Compile or build flowplayer or jwplayer to create secure token for wowza server


Hits: 10541  

I think you already read our last article for wowza mediasecurity addon package

There are 2 methods to put secure token for video players like flowplayer or jwplayer…

1. Token inside javascript code (shared secret)
2. Token inside flowplayer or jwplayer source code (compile the token inside the plugin)

The shared secret known by the server and the client only. If you really want to keep this secret you need to follow 2nd option 😉

We need to install following software in order to work on Flowplayer Flash plugins:

Flex SDK

Java Development Kit (JDK) & Apache Ant


Flow Player

You can find all steps to compile Flow Player with secure plugin here
http://flowplayer.org/documentation/developer/development-environment.html

flowplayer.core/build.properties

# you need to adjust following to point to your Flex SDK
flex3dir=D:/flex_sdk

# change following to point to .exe files when running on Windows
mxmlc_bin= D:/flex_sdk/bin/mxmlc.exe
compc_bin= D:/flex_sdk/bin/compc.exe
asdoc_bin= D:/flex_sdk/bin/asdoc.exe

devkit-dir=../flowplayer.devkit
plugins.dir=../

site.dir=D:/flowplayer/deploy
js.deploy.dir=${site.dir}/deploy/js
deploy.dir=${site.dir}/content/swf

Modify Secure Token here… flowplayer.securestreaming/src/actionscript/org/flowplayer/securestreaming/Config.as

private var _token:String = “#hv%h0#s@1”; // token that used for wowza server security 😉

Building the player
CD (change directory) to the flowplayer.core and execute ant at the command prompt:

> ant

If you see “BUILD SUCCESSFUL”, the build was successful and you have a working player located at flowplayer.core/build/flowplayer.swf.

Get more help here…
http://flowplayer.org/documentation/developer/building-in-plugins.html
http://flowplayer.org/documentation/developer/writing-flash-plugins.html


JW Player

You can find all steps to compile JW Player with secure token here http://developer.longtailvideo.com/trac/browser/trunk/fl5/README.txt


So, now the question is how to protect SWF (with token) files from hotlink, theft, download and Leech

Contact us to add SecureToken protection to JWPlayer and FlowPlayer for Wowza, Red5, FMS media files.

Email: svnlabs[at]gmail.com
Mobile: +919928673578
Skype: svnlabs
MSN: svnlabs[at]hotmail.com

Facebook Application Development with Graph API Cookbook


Hits: 2964  

Facebook Application Development with Graph API Cookbook is written by Shashwat Srivastava, Apeksha Singh for Facebook developers ranging from novice to expert. It uses PHP, HTML and jQuery, the most commonly used platforms, to build applications in Facebook.

Facebook Application Development with Graph API Cookbook

Learn from this book:

  • Install Facebook PHP SDK and register your Facebook application
  • Obtain access token and perform session validation for current Facebook user
  • Work with the newly launched Facebook Graph API
  • Get an insight into the core of Facebook application development such as creating a post on wall, publishing pictures, tagging users, etc
  • Connect to Facebook Social Graph by using Facebook PHP and JavaScript SDKs
  • Leverage Facebook Query Language in order to obtain complex Facebook user data
  • Use Facebook Dialogs to enhance user experience
  • Power your websites with Facebook Social Plugins
  • Define your website content to Facebook Graph using Facebook Open Graph Protocol
  • Handle transactions through Facebook Credits
  • Use XFBML tags to enrich your application
  • Extract Facebook application analytics data
  • Create your own applications, from tip to toe, with ready to use code

Get your copy here… http://www.packtpub.com/facebook-application-development-with-graph-api-cookbook/book

Fatal error: Allowed memory size of 8388608 bytes exhausted


Hits: 4235  

PHP Fatal error: Allowed memory size of X bytes exhausted (tried to allocate Y) in file.php

1. memory_limit globally from php.ini
memory_limit = 64M

2. memory_limit using .htaccess
php_value memory_limit 64M

3. memory_limit inside a php script.
ini_set(‘memory_limit’, ’64M’);

To change the memory limit for one specific script by including a line such as this at the top of the script:

ini_set("memory_limit","64M");

The 12M sets the limit to 64 megabytes. If this doesn’t work, keep increasing the memory limit until your script fits.

You can change permanently for all PHP scripts running on the server by adding a line to the server’s php.ini file:

memory_limit = 64M

Keep in mind that a huge memory limit is a poor substitute for good coding. Some applications like data scrappers are run infrequently and require lots of memory like importing and processing a big data file using RSS, XML, JSON API etc.

Make sure each time script required server memory resources; need to release them when script/process finish first iteration to save memory uses 😉

PHP function might help….

1. unset — Unset a given variable

2. memory_get_usage — Returns the amount of memory allocated to PHP

3. memory_get_peak_usage — Returns the peak of memory allocated by PHP

4. __destruct your object references upon disposing of an object

5. The Alternative PHP Cache (APC) is a free and open opcode cache for PHP. Its goal is to provide a free, open, and robust framework for caching and optimizing PHP intermediate code.

6. Turn PHP scripts into Linux daemons

7. Xdebug’s Profiler is a powerful tool that gives you the ability to analyze your PHP code and determine bottlenecks or generally see which parts of your code are slow and could use a speed boost.


<?php
class Memory
{
public $var='3.1415962654';
}

$baseMemory = memory_get_usage();

for($i=0;$i<=100000;$i++)
{
$a=new Memory;
$a->self=$a;
if($i%500===0)
{
echo sprintf('%8d: ',$i), memory_get_usage() - $baseMemory,"\n";
}
}
?>

Fatal error: Allowed memory size of 8388608 bytes exhausted

 

Quality is not a product.. it is a process

Source:

http://phplens.com/lens/php-book/optimizing-debugging-php.php
http://php.net/manual/en/features.gc.performance-considerations.php

PHP Cloud


Hits: 5182  

Introduction to the Zend Developer Cloud

PHPCloud help to Develop, Deploy and Manage PHP Applications in to Any Cloud (Amazon, Rackspace, RightScale, IBM SmartCloud) using Zend Application Fabric.

Demo: http://svnlabs.my.phpcloud.com
Contact: http://blog.svnlabs.com/contact/