header


Some very basic php tasks

These are simple php examples It includes login , session , echoing , and my sql connection. Im using this simple form for first part. The form is posting to this page but the form action should be action="login.php" or whatever page is required its more secure to keep the login details of the main pages.

This is to include another page

<?php include "login.php"; ?>



basic echo text

<?php echo 'this is used to echo text' ;?>

this is used to echo text


The form for the posting

Enter some text into the username the red text is the output. Its simple echoing and a very unsecure login but the $username is the key make that more secure.


<form action="" method="post">
<label>UserName :</label>
<input name="username" placeholder="username" type="text">
<input name="submit" type="submit" value="Login">
</form>
<?php $username=$_POST['username'];?>




The results of the form post php echoing


Echoing your input

<?php echo $username ; ?>



Echoing your input in a sentence

<?php echo 'Hello ' . $username . ' how are you'; ?>


Hello how are you


Using the if statement

<?php if ($_POST['username']) { echo 'Hello';}?>



<?php
if ($_POST['username'=='letmein']) { echo 'You are logged in';}
else {echo 'not logged in enter this letmein'}; ?>


not logged in enter this letmein



Using the session

The sessin code must be at the very top of the page
This is same form again but renamed input from username to sesstry. The name to enter to start the session is yourin.
click try home notice you cant access the home page. Now enter any name into the box below you will see the session name echoed. Now enter yourin into the session box then click the link again

<?php
session_start();
$_SESSION['login2'] = $_POST['sesstry'];
?>


This is now the session name


This code goes on any pages you only want the person in the session to view notice the ! before the == this stands for not equal to. if you remove this it will mean equal to. So when you post the name it creates the session name. The try home link has session name letmein so if thats not the session name it sends you back to this page.

Home.php
<?PHP
session_start();
if ($_SESSION['login2'] !== 'yourin')
{header('Location: index.php');}
else{}
?>


Using an IP address for session name

This is your ip address being used as the session name. It could be used to block certain ip addresses such as if you notice some hacking on your site eg spamming emails bots etc. Also usefull using in mysql log IPs and block bad ones for database comments members or as here a session name.


<?PHP
$_SESSION['ipadd'] = $_SERVER["REMOTE_ADDR"];
echo $_SESSION['ipadd'];
?>


3.226.251.81


connecting to a mysql database

<?php
$db_host = "localhost";
$db_username = "root unless changed";
$db_pass = "your mysql password";
$db_name = "the database name";

@mysql_connect ("$db_host","$db_username","$db_pass") or die ("could not connect");
@mysql_select_db ("$db_name") or die ("no database");
?>



displaying info from a database


<?php $strSQL = "SELECT * FROM sampler ";
$rs = mysql_query($strSQL);
echo "<table border=2><tr><th>id</th><th>name</th><th>age</th></tr>";
while($row = mysql_fetch_array($rs)) { echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<tr>";} echo "</table>";?>



idnameage
1adam43
2arther43
3charles25
4paul62
5bill11
6jeff12
The total of all the ages

<?php
$query = "SELECT SUM(age) FROM sampler";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo $row['SUM(age)'];}
mysql_close();
?>

196

add to database

<form action="posting.php" method="POST">
<input type = "submit" name="basicno" value = "Add1">
</form>
<?php include "../datalogin.php";
if ($_POST['basicno'])
{mysql_query ("UPDATE `sampler` SET `age` = `age`+1 WHERE id='1'");
header('Location: index.php');
}?>

Look at the age of adam it goes up by 1 on each click of the button also the add all ages goes up to.