0% found this document useful (0 votes)
79 views9 pages

Dosti Movie

The document discusses various PHP functions and concepts for working with forms, cookies, sessions, and MySQL databases. It includes examples of how to: 1. Use the $_POST superglobal to collect form data submitted via the POST method 2. Create and access numeric and associative arrays 3. Set, retrieve, and destroy cookies 4. Start PHP sessions and store/retrieve session variables 5. Connect to a MySQL database and perform queries like SELECT, INSERT, UPDATE, and DELETE 6. Build a basic user registration and login system using PHP and MySQL

Uploaded by

habibrehman423
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views9 pages

Dosti Movie

The document discusses various PHP functions and concepts for working with forms, cookies, sessions, and MySQL databases. It includes examples of how to: 1. Use the $_POST superglobal to collect form data submitted via the POST method 2. Create and access numeric and associative arrays 3. Set, retrieve, and destroy cookies 4. Start PHP sessions and store/retrieve session variables 5. Connect to a MySQL database and perform queries like SELECT, INSERT, UPDATE, and DELETE 6. Build a basic user registration and login system using PHP and MySQL

Uploaded by

habibrehman423
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 9

Post function

<form action="welcome.php" method="post">

Name: <input type="text" name="fname" />

Age: <input type="text" name="age" />

<input type="submit" />

</form>

The "welcome.php" file can now use the $_POST function to collect form data (the names of the form
fields will automatically be the keys in the $_POST array):

Welcome <?php echo $_POST["fname"]; ?>!<br />

You are <?php echo $_POST["age"]; ?> years old.

ARRAY

An array stores multiple values in one single variable.

Numeric Arrays

A numeric array stores each array element with a numeric index.

There are two methods to create a numeric array.

1. In the following example the index are automatically assigned (the index starts at 0):

$cars=array("Saab","Volvo","BMW","Toyota");

2. In the following example we assign the index manually:

$cars[0]="Saab";

$cars[1]="Volvo";

$cars[2]="BMW";

$cars[3]="Toyota";

In the following example you access the variable values by referring to the array name and index:

<?php

$cars[0]="Saab";

$cars[1]="Volvo";

$cars[2]="BMW";

$cars[3]="Toyota";

echo $cars[0] . " and " . $cars[1] . " are Swedish cars.";
?>

The code above will output:

Saab and Volvo are Swedish cars.

Associative Arrays

An associative array, each ID key is associated with a value.

In this example we use an array to assign ages to the different persons:

$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);

Example 2

This example is the same as example 1, but shows a different way of creating the array:

$ages['Peter'] = "32";

$ages['Quagmire'] = "30";

$ages['Joe'] = "34";

The ID keys can be used in a script:

<?php

$ages['Peter'] = "32";

$ages['Quagmire'] = "30";

$ages['Joe'] = "34";

echo "Peter is " . $ages['Peter'] . " years old.";

?>

The code above will output:

Peter is 32 years old.

What is a Cookie?

A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's
computer. Each time the same computer requests a page with a browser, it will send the cookie too.
With PHP, you can both create and retrieve cookie values.

How to Create a Cookie?

The setcookie() function is used to set a cookie.

Note: The setcookie() function must appear BEFORE the <html> tag.

Syntax

setcookie(name, value, expire, path, domain);


Example 1

In the example below, we will create a cookie named "user" and assign the value "Alex Porter" to it.
We also specify that the cookie should expire after one hour:

<?php

setcookie("user", "Alex Porter", time()+3600);

?>

How to Retrieve a Cookie Value?

The PHP $_COOKIE variable is used to retrieve a cookie value.

In the example below, we retrieve the value of the cookie named "user" and display it on a page:

<?php

// Print a cookie

echo $_COOKIE["user"];

// A way to view all cookies

print_r($_COOKIE);

?>

Starting a PHP Session

Before you can store user information in your PHP session, you must first start up the session.

Note: The session_start() function must appear BEFORE the <html> tag:

<?php session_start(); ?>

Storing a Session Variable

The correct way to store and retrieve session variables is to use the PHP $_SESSION variable:

<?php

session_start();

// store session data

$_SESSION['views']=1;

?>

<html>

<body>
<?php

//retrieve session data

echo "Pageviews=". $_SESSION['views'];

?>

Destroying a Session

If you wish to delete some session data, you can use the unset() or the session_destroy() function.

The unset() function is used to free the specified session variable:

<?php

unset($_SESSION['views']);

?>

You can also completely destroy the session by calling the session_destroy() function:

<?php

session_destroy();

?>

Mysql Connect

<?php

$con = mysql_connect("localhost","peter","abc123");

if (!$con)

die('Could not connect: ' . mysql_error());

?>

Mysql Insert

Insert Data From a Form Into a Database

Now we will create an HTML form that can be used to add new records to the "Persons" table.

Here is the HTML form:

<html>

<body>

<form action="insert.php" method="post">


Firstname: <input type="text" name="firstname" />

Lastname: <input type="text" name="lastname" />

Age: <input type="text" name="age" />

<input type="submit" />

</form>

</body>

</html>

<?php

$con = mysql_connect("localhost","peter","abc123");

if (!$con)

die('Could not connect: ' . mysql_error());

mysql_select_db("my_db", $con);

$sql="INSERT INTO Persons (FirstName, LastName, Age)

VALUES

('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

if (!mysql_query($sql,$con))

die('Error: ' . mysql_error());

echo "1 record added";

mysql_close($con)

?>

Mysql Select or display

<?php

$con = mysql_connect("localhost","peter","abc123");

if (!$con)

die('Could not connect: ' . mysql_error());


}

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM Persons");

echo "<table border='1'>

<tr>

<th>Firstname</th>

<th>Lastname</th>

</tr>";

while($row = mysql_fetch_array($result))

echo "<tr>";

echo "<td>" . $row['FirstName'] . "</td>";

echo "<td>" . $row['LastName'] . "</td>";

echo "</tr>";

echo "</table>";

mysql_close($con);

?>

Mysql Where

<?php

$con = mysql_connect("localhost","peter","abc123");

if (!$con)

die('Could not connect: ' . mysql_error());

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM Persons

WHERE FirstName='Peter'");

while($row = mysql_fetch_array($result))
{

echo $row['FirstName'] . " " . $row['LastName'];

echo "<br />";

?>

Mysql update

<?php

$con = mysql_connect("localhost","peter","abc123");

if (!$con)

die('Could not connect: ' . mysql_error());

mysql_select_db("my_db", $con);

mysql_query("UPDATE Persons SET Age = '36'

WHERE FirstName = 'Peter' AND LastName = 'Griffin'");

mysql_close($con);

?>

Mysql Delete

<?php

$con = mysql_connect("localhost","peter","abc123");

if (!$con)

die('Could not connect: ' . mysql_error());

mysql_select_db("my_db", $con);

mysql_query("DELETE FROM Persons WHERE LastName='Griffin'");

mysql_close($con);

?>

Creating a login system

Registration
<form action="register.php" method="post">

Name: <input type="text" name="username" />

Age: <input type="text" name="password" />

<input type="submit" />

</form>

register.php

<?php

mysql_connect("localhost", "root", "") or die(mysql_error());

mysql_select_db("store") or die(mysql_error());

$username=$_POST[username];

$pass=$_POST[password];

mysql_query("INSERT INTO users

(username, password) VALUES('$username', '$pass' ) ")

or die(mysql_error());

Login

<form action="login.php" method="post">

Name: <input type="text" name="username" />

Age: <input type="text" name="password" />

<input type="submit" />

</form>

Login.php

<?php

mysql_connect("localhost", "root", "") or die(mysql_error());

mysql_select_db("store") or die(mysql_error());

$username=$_POST[username];

$pass=$_POST[pass];

$sql="SELECT * FROM users WHERE username='$username' and pass='$pass'";

$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count==1){
session_start();

$_SESSION['logged'] = 1;

$_SESSION['username'] = $username;

header('Location: http://localhost/store/');

else {

echo "Wrong Username or Password";

?>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy