Php Code Blocks

Automatic Intelligent Random Password Generator

It is something usual to produce a random password. It is used when a user has forgotten his password and need a new password; or in sum login register forms which the user can not choose his password at first. This script is an intelligent password generator. You set the number of letters and numbers used in the password phrase, and it randomly generates it. If you have a special name, number or character that must be in the phrase, you can insert it in the form. The script randomly select the place of numbers, letters and special phrases.

PHP CODE :

<?php
if (isset($_POST['Gen'])){
$num=$_POST['num'];
$let=$_POST['let'];
$schar=$_POST['schar'];
$sname=$_POST['sname'];
$snum=$_POST['snum'];

$l=strlen($sname)+strlen($snum)+strlen($schar)+$let+$num;
$max=pow(10,$num)-1;
$cnum=rand(1,$max);
if (strlen($cnum)<$num) $cnum=$cnum*pow(10,$num-strlen($cnum));
$clet='';
for ($i=1;$i<=$let;$i++){
$r=rand(1,2);
if ($r==1) 
$c=chr(rand(65,90));
else
$c=chr(rand(97,122));
$clet.=$c;
}
$pass_a=array(0,$schar,$sname,$snum,$clet,$cnum);
$pass_b[0]=0;
for ($i=1;$i<=5;$i++){
$used=true;
while ($used==true){
$n=rand(1,5);
$used=false;
for ($j=1;$j<=count($pass_b)-1;$j++)
if ($n==$pass_b[$j]) $used=true;
}
$pass_b[]=$n;
}
}
?>


<form action="" method="post" >
<table style="border: 1px solid #000000;width: 291px; height: 77px;" align="center">
<tr>
<td style="padding:5px; font-family: 'Times New Roman', Times, serif;font-size: 14pt; text-align: left; width: 1046px; color: #2214B9;border-style: solid;border-width: 1px; background-color: #FFFFE3; height: 38px;">
Numbers Count:</td>
<td style="border-style: solid;border-width: 1px; height: 39px;padding-left: 8px; width: 435px; padding-right: 5px; padding-top: 5px; padding-bottom: 5px; background-color: #FFFFE3;">
<input name="num" style="font-family: 'Times New Roman', Times, serif; font-size: 14pt; width: 91px; text-align: center; color: #0AA24F;" value="<?php if (isset($_POST['Gen'])) echo $_POST['num'] ?>"></td>
</tr>
<tr>
<td style="padding:5px; width: 1046px; border-style: solid;border-width: 1px;text-align: left; height: 39px; font-size: 14pt; background-color: #FFFFE3; font-family: 'Times New Roman', Times, serif;">
<span style="color: #2214B9">Letters</span><span style="font-family: 'Times New Roman', Times, serif; color: #2214B9"> 
Count:</span></td>
<td style="border-style: solid;border-width: 1px; height: 39px;padding-left: 8px; width: 435px; padding-right: 5px; padding-top: 5px; padding-bottom: 5px; background-color: #FFFFE3;">
<input name="let" style="font-family: 'Times New Roman', Times, serif; font-size: 14pt; width: 91px; text-align: center; color: #0AA24F;" value="<?php if (isset($_POST['Gen'])) echo $_POST['num'] ?>"></td>
</tr>
<tr>
<td style="padding:5px; width: 1046px; border-style: solid;border-width: 1px;text-align: left; height: 39px; font-size: 14pt; background-color: #FFFFE3; font-family: 'Times New Roman', Times, serif; color: #2214B9;">
Special Character:</td>
<td style="border-style: solid;border-width: 1px; height: 39px;padding-left: 8px; width: 435px; padding-right: 5px; padding-top: 5px; padding-bottom: 5px; background-color: #FFFFE3;">
<input name="schar" style="font-family: 'Times New Roman', Times, serif; font-size: 14pt; width: 91px; text-align: center; color: #0AA24F;" value="<?php if (isset($_POST['Gen'])) echo $_POST['schar'] ?>"></td>
</tr>
<tr>
<td style="padding:5px; width: 1046px; border-style: solid;border-width: 1px;text-align: left; height: 39px; font-size: 14pt; background-color: #FFFFE3; font-family: 'Times New Roman', Times, serif; color: #2214B9;">
Special Name:</td>
<td style="border-style: solid;border-width: 1px; height: 39px;padding-left: 8px; width: 435px; padding-right: 5px; padding-top: 5px; padding-bottom: 5px; background-color: #FFFFE3;">
<input name="sname" style="font-family: 'Times New Roman', Times, serif; font-size: 14pt; width: 91px; text-align: center; color: #0AA24F;" value="<?php if (isset($_POST['Gen'])) echo $_POST['sname'] ?>"></td>
</tr>
<tr>
<td style="padding:5px; width: 1046px; border-style: solid;border-width: 1px;text-align: left; height: 39px; font-size: 14pt; background-color: #FFFFE3; font-family: 'Times New Roman', Times, serif; color: #2214B9;">
Special Number:</td>
<td style="border-style: solid;border-width: 1px; height: 39px;padding-left: 8px; width: 435px; padding-right: 5px; padding-top: 5px; padding-bottom: 5px; background-color: #FFFFE3;">
<input name="snum" style="font-family: 'Times New Roman', Times, serif; font-size: 14pt; width: 91px; text-align: center; color: #0AA24F;" value="<?php if (isset($_POST['Gen'])) echo $_POST['snum'] ?>"></td>
</tr>
</table>
<p style="text-align: center">
<input name="Gen" style="width: 148px; height: 64px; font-size: 25pt; font-family: 'Times New Roman', Times, serif; color: #119B23" type="submit" value="Generate"></p>
</form>
<?php if (isset($_POST['Gen'])){ ?>
<table style="width: 301px" align="center">
<tr>
<td style=" padding:5px; background-color: #FABCAA; text-align: center; height: 64px;">
<span style="font-family: 'Times New Roman', Times, serif; font-size: 16pt; color: #2214B9">
Your Generated Password is:</span><br>
<span style="font-size: 25pt; font-family: 'Times New Roman', Times, serif; color: #3FAC0F">
<?php echo $pass_a[$pass_b[1]].$pass_a[$pass_b[2]].$pass_a[$pass_b[3]].$pass_a[$pass_b[4]].$pass_a[$pass_b[5]] ?>
</span></td>
</tr>
</table>
<?php } ?>

Prohibit an IP Address

You may have visitors that hurt your site and you want to block their access. Here is a code that get this ability to you. just replace the $blocked with the IP that must be blocked.


<?php
$blocked="127.0.0.1";
if (ereg($blocked,$ip)) die('
?>
<p style="text-align: center; color: #FF0000; font-size: 32pt;">
<strong>Your IP is Blocked!</strong>
</p>
<?php
');
?>

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.
Add the following code to a page:
<?php
function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
?>

You can now get the current page URL using the line:

<?php
  echo curPageURL();
?>

Sometimes it is needed to get the page name only. The following example shows how to do it: 
<?php
function curPageName() {
 return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}

echo "The current page name is ".curPageName();
?>

Welcome

Welcome to php code blocks in this blog you find an cool and interesting php codes explained you might needed with your coding so it's alot of coding time.