Kuching Open Source Community

 » Community efforts in sharing open source & Linux knowledge...

Skip to content

Simple but useful PHP codes

Coding, coding, coding... bugs, bugs, bugs...

Simple but useful PHP codes

Postby filuren » Wed, 19 Apr 2006 @ 8:54pm

I am going to paste some simple but useful PHP codes that I am always looking for :P

I'll start with simple URL forwarding. This works way better than HTML meta refresh tags to a specify URL.

Create a new file .php and paste this sample:
Code: Select all
<?php
header("Location: http://www.kuchingosc.org");
?>


Whenever you access this php file, it will redirect you to kuchingosc.org

Will paste next tips next :wink:
User avatar
filuren
Site Admin
 
Posts: 178
Joined: Wed, 12 Apr 2006 @ 11:34am

Postby manteros » Wed, 10 May 2006 @ 9:06am

This code is used to display words in php file u guys can used it to put html format also can...

example 1
Code: Select all
<?
echo'Hello World';
?>


example 2
Code: Select all
<?
echo '
     <html>
       <body>
      Hello World
       </body>
     </html>
     ';
?>
Manteros Blogspot | Alumni SMTM blogspot
-------|||||||------
_oo_{^_^}_oo_
Image
User avatar
manteros
 
Posts: 178
Joined: Thu, 13 Apr 2006 @ 3:35pm
Location: Kuching

Postby NeophyteHeaven » Mon, 22 May 2006 @ 6:27pm

Code: Select all
<?php
if (getenv(HTTP_X_FORWARDED_FOR)) {
$ip   = getenv('HTTP_X_FORWARD_FOR');
$host = gethostbyaddr($ip);
} else {
$ip   = getenv('REMOTE_ADDR');
$host = gethostbyaddr($ip);
}
?>


try this..getting IP address. More info here..http://www.php.net/gethostbyaddr
Image
User avatar
NeophyteHeaven
 
Posts: 69
Joined: Mon, 22 May 2006 @ 6:15pm
Location: Kuching Sarawak

Postby manteros » Wed, 31 May 2006 @ 8:37am

hehehe.. now the real stuff...

First you need to create a priviledge user for your database then you create your own database. the table that i used is test as you can find it in $table that i declare in login.php you may put to field of username2 and password2.


connect.php
<?
/*
This is Database Connection
*/
$localhost = "localhost";
$userdb="test";
$passdb="test";
$database="test";

mysql_connect ($localhost, $userdb, $passdb) or die ('My SQL Error: ' . mysql_error());
mysql_select_db ($database);
?>


connect.php is a connection to you database as you can see there you need to create a user name and password also you database in you mysql. Example below:-

username = test
password = test
database name = test

mysql_connect ($localhost, $userdb, $passdb) or die ('My SQL Error: ' . mysql_error());


Here is a statement for you to connect to your database. In php we using mysql_connect statement to connet to database. ('My SQL Error: ' . mysql_error() is an error statement that appear if the database that connected is available or not and also other error. This is the error handling.

login.php
<?
require_once "connect.php";

$table="test";

$username=$_POST['username2'];
$password=$_POST['password2'];
$stuff = mysql_query("SELECT * FROM $table WHERE username1='".$username."' AND password1='".$password."'") or die("MySQL Login Error: ".mysql_error());

if(isset($_POST['username2'],$_POST['password2']))
{
if (mysql_num_rows($stuff) > 0) {
$row=mysql_num_rows($stuff);

if(isset($_POST['remember']))
{
setcookie('username2', $_POST['username2'], time() + 31536000) or die('Could not set login cookie');
setcookie('password2', ($_POST['password2']), time() + 31536000) or die('Could not set login cookie');
}
else
{
setcookie('username2', $_POST['username2']) or die('Could not set login cookie');
setcookie('password2', ($_POST['password2'])) or die('Could not set login cookie');
}
echo "<p><form action=home.php><p align=center>Welcome <b>$username</b>.<br> Please press continue to continue.</p><p align=center><input class=button type=submit value=Continue></p></form>";

//echo "You are logged in as $username <br>";
}else{
echo'
<!-- ***** Below is all the HTML statement *****-->
<table align=center>
<tr>
<td align=center colspan=2>
<font size=10>Welcome</font>
</td>
</tr>
<tr>
<td colspan=2><font color=red>*</font>
Please Enter Username and Password</td>
</tr>
<form method=POST>
<tr>
<td>User name</td><td><input type=text name=username2></td>
</tr>
<tr>
<td>Password</td><td> <input type="password" name="password2" size="20" /></td>
</tr>

<tr>
<td align=center colspan=2><input type=submit value=LogIn>
<a href=signup.php>Sign Up</a>
</td>
</tr>
</form>
</table>
';}
}else {
echo"
<!-- ***** Below is all the HTML statement *****-->
<table align=center>
<tr>
<td align=center colspan=2>
<font size=10>Welcome</font>
</td>
</tr>
<form method=POST>
<tr>
<td>User name</td><td><input type=text name=username2></td>
</tr>
<tr>
<td>Password</td><td> <input type=password name=password2 size=20 /></td>
</tr>

<tr>
<td align=center colspan=2><input type=submit value=LogIn>
<a href=signup.php>Sign Up</a>
</td>
</tr>
</form>
</table>
";
}
?>



home.php
<?
ob_start();
//checking username and password from cookie
if(!isset($_COOKIE['username2'],$_COOKIE['password2']))
{ /*if there username2 and password2 not the same it will ask login first before enter*/
echo '<p align="center">Please <a href="login.php">login</a> first before Enter.</p>';}
else{
/*
After this echo'
put your own HTML code
*/
echo'
<p align=right>
<a href></a>
<a href=logout.php>Logout</a>
</p>
<div align="center">
<center>

<table border=1>
<tr>
<td colspan=2>test</td>
</tr>
<tr>
<td>test</td>
<td>test</td>
</tr>
</table>

</center>
</div>
';
}

?>



logout.php
<?php

/********************
* logout.php *
********************/

ob_start();

if(!isset($_COOKIE['username2'],$_COOKIE['password2']))
die('<form action="login.php"><p align="center">You are already logged out.<p align="center">Do you want to login?</p> <p align="center"><input class="button" type="submit" value="login"></p></form>');

setcookie('username2', $_COOKIE['username2'], time() - 3600);
setcookie('password2', $_COOKIE['password2'], time() - 3600);

echo '
<form action="login.php"><p align="center">You are already logged out.<p align="center">Do you want to login?</p> <p align="center"><input class="button" type="submit" value="login"></p></form>
';

?>


Well i think this is the simplest login and logout program that i've made well you guys can try and have fun also if you guys don't understand some of the statement just post it here....hope this will help freebies guys... to make their own web based programming....
Manteros Blogspot | Alumni SMTM blogspot
-------|||||||------
_oo_{^_^}_oo_
Image
User avatar
manteros
 
Posts: 178
Joined: Thu, 13 Apr 2006 @ 3:35pm
Location: Kuching

Re: Simple but useful PHP codes

Postby Virion » Fri, 27 Jun 2008 @ 4:27pm

I got some tips too. If you want to display a dynamic page, for example, you are now at "index.php", you are displaying a sub-page (ex: news.php) using an include function, and you want to switch from news.php to, for example, about.php without refreshing the page (still displaying on index.php), you can follow the steps below:

1. make sure that you are using include('news.php'); on index.php.
2. remove include('news.php'); and replace with

Code: Select all
<?php
         $page = $_GET['page'];
         if (!empty($page))
         {
            include($page);
         }
         else
         {
            include('news.php');
         }
         
         ?>

it means that it it will include whatever file the $page is storing. for default, $page = 'news.php', however, if you change it to others (ex. about.php), it will change the page dynamically, without even need to refresh the page.

ok let me explain it further by using code. now, assume that you have a navigation bar, and you have a hyperlink named as "About". you want the page to switch to "about.php" when people clicked it. you can use the following PHP code:

Code: Select all
<?php
echo '<a href="' . $_SERVER['PHP_SELF'] . '?page=about.php">About</a>';
?>


the code ?page=about.php will change the variable $page from "news.php" to "about.php". so now $page is no longer "news.php" but "about.php". you will see the page changed dynamically without changing/refreshing the whole page. hope you understand what i'm talking about (sorry i can't explain well). enjoy coding. ;)
User avatar
Virion
 
Posts: 21
Joined: Thu, 26 Jun 2008 @ 6:30pm


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron