Archive for May, 2007

Filed Under (Javascript Code Samples) by Anoop on May-31-2007
Code (javascript)

<HTML>
 <HEAD>
  <TITLE> Javascript Character Count by WebSewak.com </TITLE>
<script language="Javascript">
function counterUpdate(opt_countedTextBox, opt_countBody, opt_maxSize) {
        var countedTextBox = opt_countedTextBox ? opt_countedTextBox : "counttxt";
        var countBody = opt_countBody ? opt_countBody : "countBody";
        var maxSize = opt_maxSize ? opt_maxSize : 1024;

        var field = document.getElementById(countedTextBox);

        if (field && field.value.length >= maxSize) {
                field.value = field.value.substring(0, maxSize);
        }
        var txtField = document.getElementById(countBody);
                if (txtField) { 
                txtField.innerHTML = field.value.length;
        }
}
</script>
 </HEAD>

 <BODY>
  <textarea id="counttxt" name="counttxt" rows="10" onkeyup="counterUpdate('counttxt', 'countBody','30');"></textarea>
 
  You typed <B><span id="countBody">0</span></b> characters
  Max. Length : 30 Chars
 </BODY>
</HTML>
 


Filed Under (PHP Code Samples) by Srikanta on May-30-2007

This random password generator generates the password upto a desired length and includes the desired set of characters. It ensures that you will get atleast one random character from the character type you have choosen.

Click here to see the demo of the Random Password Generator

Code (php)

<?
$Source[0] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$Source[1] = "0123456789";
$Source[2] = "!@#$%^*_-+=?|:";
if($_POST['submit'] == 1)
{
        if($_POST['alpha'] == '' && $_POST['num'] == '' && $_POST['other'] == '')
        {
                $Error = "Please provide 'Password Type'.";
        }
        else
        {
                if($_POST['alpha'] == '1' )
                {
                        $Use[] = $Source[0];
                }
                if($_POST['num'] == '1' )
                {
                        $Use[] = $Source[1];
                }
                if($_POST['other'] == '1' )
                {
                        $Use[] = $Source[2];
                }
                $Passwordlen = $_POST['passwordlen'] ;
                if($Passwordlen >= count($Use))
                {
                        $Min = 0;
                        while($i < $Passwordlen)
                        {
                                $Max = strlen($Use[$i % count($Use)])-1;
                                $Rand = rand($Min,$Max);
                                $Password .= substr($Use[$i % count($Use)],$Rand,1);
                                $i++;
                        }
                }
                else
                {
                        $Error="Please enter password length  greater than number of 'Password Type' selected.";
                }
        }
}
?>
<html>
<head>
<style>
<!--
.box{
        background-color:#ba0808;
        color:#ffdf9a;
        font-size:20px;
}
.header{
        background:#6f0006;
        color:#ffa200;
}
.password{
        background:#630102;
        color:#ffa200;
}
-->
</style>

</head>
<body>
<CENTER>
<div class="box">
<form method="POST">
<div class="header">
<h1>Password Generator</h1>
</div>
<table class="box">
<?
if($Error != '')
{
?>
<tr>
<td>
                <font color="#ffa200" size="6"><?=$Error;?></font>
        </td>
</tr>

<?
}
?>
<tr>
<td>
                Enter your password length:
<input type="text" name="passwordlen" size="2" maxlength="2" value="<?=$_POST['passwordlen'];?>">
</td>
</tr>
<tr>
<td>
                Please Select Characters to be used in the password:
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="alpha" value="1" <?
if($_POST['alpha']!='')
{
        echo "checked";
}
?>
>Use Alphabets (e.g., QlqzkBKrod )
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="num" value="1" <?
if($_POST['num']!='')
{
echo "checked";
}
?>
>Use Numbers (e.g., 5122401523 )
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="other" value="1" <?
if($_POST['other']!='')
{
echo "checked";
}
?>
>Use Other Characters (e.g., ?=:!?%^-@|  )
</td>
</tr>
<tr>
<td>
<input type="hidden" name="submit" value="1">
<input type="submit" value="Generate">
        </td>
</tr>

</TABLE>
</form>
<div class="password">
<?
if($Password != '')
{
?>
<h2>Generated Password: <?=$Password;?></h2>

<?
}
else if($Error != '')
{
?>
<h2>Error : <?=$Error?></h2>

<?
}
else
{
?>
<h3>Password Generater</h3>

<?
}
?>
</div>
</div>

</CENTER>
</body>
</html>
 



Filed Under (Articles, PHP Code Samples) by Virender on May-29-2007

Cookies and Sessions both fulfill the purpose of storing data across the pages of a website. Both have their own advantages.

Cookies are stored on the client side and can be set to a long lifespan, which means that data stored in a cookie can be stored for months if not years. Cookies work fine with a web application installed on a cluster of web server, whereas sessions are stored on the server, means if one web server is handling the request, the other web server on the cluster will not have access to the information stored.

As Sessions are stored on web server, this gives extra security and freedom. We do not transmit session data with each page, just an ID and the data is loaded from the local file stored on the server. Clients do not know about the information we store about them, which is not the case with Cookies.

Many browsers have a limit for the Cookies as to stop the wastage of bandwidth that is caused by meaningless Cookies, but that is not the case with Sessions. They are stored on the server, hence they can be of any size. One can use them as per requirement. If he wants to store information until visitor comes back next day then Cookies are the way to go. If some part of cookie data is crucial then we can store it in database and just store an ID in the Cookie to reference the data. If not, then use Sessions, as they are little easier to use and do not require their entire data to be sent with each page. Sessions get cleaned as soon as visitor closes the web browser.

Let’s see how to use Cookies in a web page

The following script will store a Cookie labeled test on the client side with a value as “IsOn”. Please make sure that you set cookie values before sending any output to browser.

Code (php)

<?php
        setcookie("test", "IsOn", time() + (60 * 60));// variable name = test, value = "IsOn" and cookie life is one minute from current time. If you don't set cookie life, it will be expired after browser is closed.
?>
 

Now that our Cookie is set up, we can use it on every page of the site.

Code (php)

<?php
   echo $_COOKIE['test']; //displays "IsOn"
?>
 

If the above code shows nothing in output, then consider it as cookies being disabled or cookies improperly set. As you can see, we access information stored in cookies by using $_COOKIE[] array.

To destroy Cookies forcefully, we can use the following script:

Code (php)

<?php
        setcookie("test", "IsOn", time() - (60 * 60)); // setting a cookie life to a time that is past will actually expire the cookie instantly
?>
 

Now, let’s see how to use Sessions in a web page:

Code (php)

<?php
    session_start(); // this needs to be declared on the top of every page, specially before sending any output to the browser.
    $_SESSION['Check'] = 'test';
    echo $_SESSION['Check'];// displays "test" on any page of that web site
?>
 

We can use sessions on same page. Since sessions are server-side and the users have access to only the Session Identifier.

To destroy the Sessions for the current site, we can use the following script.

Code (php)

<?php
        session_destroy();
?>
 


Filed Under (Technology Junk) by Virender on May-26-2007

Since it’s founding in the 1980s, Dell has relied on selling PCs and other products directly to consumers and business customers over the phone and Internet. It viewed direct sales as an important cost advantage over competitors who sold computers through retailers.

The strategy worked, helping Dell become the world’s leading PC maker. But recently, the round rock-based company has lost it’s lead to a revitalized Hewlett-Packard Co.

So, now Dell has planned to sell it’s PCs at Wal-Mart Stores Inc., the world’s largest retailer as to compete with it’s competitors, ruling out its own policy of selling machines only directly to customers.

They are going to showcase their two Dimension desktop computers in about 3,000 Wal-Marts beginning June 10 in the United States, Canada and Puerto Rico.

Dell spokesman Dwayne Cox said the Wal-Mart deal “represents our first step” into global retail.
“Customers want more and new ways to buy our products, and we plan on meeting their needs on a global level,” Cox said. “Offering Dell Dimensions in Wal-Mart is a great example of this approach.”

He said Dell will announce additional moves into retail in the coming quarters, but he declined to give specifics.



Filed Under (PHP Code Samples) by Anoop on May-26-2007
Code (php)

<?
function makeClickableLinks($text)
{

        $text = html_entity_decode($text);
        $text = " ".$text;
        $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
                '<a href="\\1" target=_blank>\\1</a>', $text);
        $text = eregi_replace('(((f|ht){1}tps://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
                '<a href="\\1" target=_blank>\\1</a>', $text);
        $text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
        '\\1<a href="http://\\2" target=_blank>\\2</a>', $text);
        $text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})',
        '<a href="mailto:\\1" target=_blank>\\1</a>', $text);
        return $text;
}

// Example Usage
echo makeClickableLinks("This is a test clickable link: http://www.websewak.com  You can also try using an email address like test@websewak.com");
?>
 


Filed Under (Articles, Technology Junk) by Anoop on May-23-2007

PICO accomplished!!!

Confused? Don’t be. PICO is the name of Yahoo’s Web based Messenger’s Mission. Yahoo has launched the BETA version of its Web based Messenger on 3rd May, 2007. It is easy to use, fast and has a cool interface with no download required. It consists of all basic features like chat, add friends, stealth mode, smilies, text formatting and the most useful feature “chat history”. Now you just need an internet connection to get started with yahoo Messenger, no downloads required. Just go through http://webmessenger.yahoo.com or simply type http://web.im from your browser and you are ready to go.

webim1.gif

Feedbacks from initial users are coming great. They are pleased to see Yahoo’s web based messenger. Meebo already has a combined web based messenger that worked with industry leading messengers AOL, MSN, Yahoo, Google talk. Yahoo web messenger is compatible with all the browsers like IE, Firefox, Opera, Netscape and safari etc. Yahoo has again done a great job just like Yahoo Mail Beta.

Yahoo Web messenger beta lacks some important features in comparison to desktop messenger such as file transfer, webcam, Chat rooms, Voice chat etc. But still it provides most of the necessary features required for key communication and self expressive features you need. Thumbs up to Yahoo’s new initiative.



Filed Under (Articles, Design) by Srikanta on May-22-2007
Step1 : First open Adobe Photoshop and make the desired image. For example if you are making a webpage with three parts.
page.jpg
(a)top bar (b)body (c)bottom bar
Step2 : After making this image Goto File > Edit in ImageReady(or shift+ctrl+m).
2.jpg
This will transfer the image to ImageReady.
Step3 : In ImageReady Goto Window,option before Help in menu bar.
3.jpg
If Tools & Slice option is not checked, then check it, it will
open windows of tools and slice.
4.jpg
5.jpg
Step4 :
6.jpg
Select the slice tool and cut the top bar, body and bottom bar.
Step5:
7.jpg
Select a slice then in slice window change the name of the slice.Do this with all the slices.
Step6: Now Goto File > Output Settings > HTML.
8.jpg
This will open a window Output Settings.
9.jpg
Click on Next Twice.
You will see ‘Preset:‘ dropdown menu is set to ‘Default settings‘. Second dropdown menu is set to ‘Slices‘.
Step7:
10.jpg
In ‘slice Output‘ box, select the second radio option (Generate CSS). ‘Referenced’ dropdown menu is selected
to as ‘By ID‘.Now click on ‘OK’.
Step8:
11.jpg
Now again Goto File > Save Optimized As (or ctrl+shift+alt+s) and select the drive or folder in your PC and ‘Save as type
to ‘HTML‘ or ‘HTML and Images‘.This will create an HTML file and a folder named ‘Images‘.
Step9:
12.jpg
The HTML file is having the CSS code. You can change this code according to your need.
13.jpg


Filed Under (Javascript Code Samples) by Anoop on May-22-2007
Code (javascript)

<script>
window.captureEvents(Event.CLICK);
window.onclick=sourceCheck;

function sourceCheck(e){
var el=(typeof event!=='undefined')? event.srcElement : e.target
alert(el.tagName)
}
</script>
 

Above code works fine in FireFox but not in IE.

Here is the one that works in Both:

Code (javascript)

<script type="text/javascript">
if (window.captureEvents){
window.captureEvents(Event.CLICK);
window.onclick=sourceCheck;
}
else
document.onclick=sourceCheck;

function sourceCheck(e){
var el=(typeof event!=='undefined')? event.srcElement : e.target
alert(el.tagName)
}
</script>
 


Filed Under (PHP Code Samples) by Anoop on May-22-2007
Code (php)

<?php
$URL = 'www.yah.in';
$ip = gethostbyname($URL); // Get IP address from a URL
$out = "The following URLs are equivalent (for dedicated IP addresses only): ";
$out .= $URL.', http://' . $ip . '/ http://' . sprintf("%u", ip2long($ip)) . "";
echo $out;
echo "Your IP address: ".$_SERVER['REMOTE_ADDR'];
?>
 

You can also use long2ip() function to get ip address back from a long code.



Filed Under (PHP Code Samples) by Srikanta on May-21-2007

PHP’s mktime() function doesn’t work before 1970, so here is the alternate function. Hope this helps somebody.

Code (php)

<?php
//Function to make timestamp for dates after year 1801
function timestamp($mm,$dd,$yyyy)
{
        if ($mm > 12 || $dd > 31 || $yyyy < 1800)
                return -1;
        $ysec=((($yyyy-1801)*365*24*3600)+(floor($yyyy-1801)/4)*24*3600);
        if(((($yyyy%4)==0&&($yyyy%100)!=0))||($yyyy%400==0))
        {
                $fsec = 29;
        }
        else
        {
                $fsec = 28;
        }
        if ($mm == 2 AND $dd > $fsec)
                return -1;
        switch($mm)
        {
                case 1  : $msec=0; break;
                case 2  : $msec=(31*24*3600); break;
                case 3  : $msec=(31+$fsec)*24*3600; break;
                case 4  : $msec=((31+31+$fsec)*24*3600); break;
                case 5  : $msec=((31+31+30+$fsec)*24*3600); break;
                case 6  : $msec=((31+31+30+31+$fsec)*24*3600); break;
                case 7  : $msec=((31+31+30+31+30+$fsec)*24*3600)break;
                case 8  : $msec=((31+31+30+31+30+31+$fsec)*24*3600); break;
                case 9  : $msec=((31+31+30+31+30+31+31+$fsec)*24*3600); break;
                case 10 : $msec=((31+31+30+31+30+31+31+30+$fsec)*24*3600); break;
                case 11 : $msec=((31+31+30+31+30+31+31+30+31+$fsec)*24*3600); break;
                case 12 : $msec=((31+31+30+31+30+31+31+30+31+30+$fsec)*24*3600); break;
        }
        $dsec=(($dd)*24*3600);
        return $ysec+$msec+$dsec;
}

//Example to use

$month=1;
$day=1;
$year=1914;

$time = timestamp($month,$day,$year);
if ($time != -1)
{
echo $time;// Displays 3566095200
}
else
{
echo "Invalid Date!";
}
?>