Sunteți pe pagina 1din 28

CONNECT

$var=mysql_connect(host,username,passw
ord,portnumber);
mysql_select_db(dbname,connect variable);
$result=mysql_query(querystring);
$row=mysql_fetch_row($result);
$row=mysql_fetch_array($result,MYSQL_AS
SOC);
$recordcount=mysql_num_rows($result);
STRING HANDLING
str_repeat(string,repeat)- repeats a string a specified number of
times.

<?php
echo str_repeat(".",13);
?>

str_replace(find,replace,string,count) - replaces some characters


with some other characters in a string.

<?php
echo str_replace("world","Peter","Hello world!");
?>

<?php
$arr = array("blue","red","green","yellow");
print_r(str_replace("red","pink",$arr,$i));
echo "Replacements: $i";
?>
strcmp(string1,string2) - compares two strings.
0 - if the two strings are equal
0 - if string1 is less than string2
0 - if string1 is greater than string2
<?php echo strcmp("Hello world!","Hello world!"); ?>
strlen(string) - returns the length of a string.
strtolower(string) - converts a string to lowercase.
strtoupper(string) - converts a string to uppercase
ucfirst(string) - converts the first character of a string to uppercase.
ucwords(string) - converts the first character of each word in a
string to uppercase.
strpos(string,find,start) - returns the position of the first occurrence
of a string inside another string.
$str = 'test string';
$sub_str = 'str';
if (strpos($str, $sub_str) === false)
echo "Substring not found";
else
echo "Substring exist in given string";
UI

<FORM ACTION="welcome.php"
METHOD=GET>
First Name: <INPUT TYPE=TEXT
NAME="firstname"><BR>
Last Name: <INPUT TYPE=TEXT
NAME="lastname">
<INPUT TYPE=SUBMIT VALUE="GO">
</FORM>
FORM PROCESSING

<?php
$firstname=$_GET['firstname'];
$lastname = $_GET['lastname'];
echo( "Welcome to our Web site,
$firstname $lastname!" );
?>
PUBLISHING MySQL DATA
<HTML>
<HEAD>
<TITLE> Our List of Jokes </TITLE>
<HEAD>
<BODY>
<?php

// Connect to the database server


$dbcnx = @mysql_connect("localhost",
"root", "mypasswd");
if (!$dbcnx) {
echo( "<P>Unable to connect to the " .
"database server at this time.</P>" );
exit();
}

// Select the jokes database


if (! @mysql_select_db("jokes") ) {
echo( "<P>Unable to locate the joke " .
"database at this time.</P>" );
exit();
}

?>
<P> Here are all the jokes in our database: </P>
<BLOCKQUOTE>

<?php

// Request the text of all the jokes


$result = mysql_query(
"SELECT JokeText FROM Jokes");
if (!$result) {
echo("<P>Error performing query: " .
mysql_error() . "</P>");
exit();
}

// Display the text of each joke in a paragraph


while ( $row = mysql_fetch_array($result) ) {
echo("<P>" . $row["JokeText"] . "</P>");
}

?>
INSERTING DATA TO MySQL
<FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=POST>
<P>Type your joke here:<BR>
<TEXTAREA NAME="joketext" ROWS=10 COLS=40 WRAP></TEXTAREA><BR>
<INPUT TYPE=SUBMIT NAME="submitjoke" VALUE="SUBMIT">
</FORM>
<?php
$joketext=$_POST['joketext'];
$submitjoke=$_POST['submitjoke'];

$dbcnx = @mysql_connect("localhost",
"root", "mypasswd");
if (!$dbcnx) {
echo( "<P>Unable to connect to the " .
"database server at this time.</P>" );
exit();
}

// Select the jokes database


if (! @mysql_select_db("jokes") ) {
echo( "<P>Unable to locate the joke " .
"database at this time.</P>" );
exit();
}
if ("SUBMIT" == $submitjoke) {
$sql = "INSERT INTO Jokes SET " .
"JokeText='$joketext', " .
"JokeDate=CURDATE()";
if (mysql_query($sql)) {
echo("<P>Your joke has been added.</P>");
} else {
echo("<P>Error adding submitted joke: " .
mysql_error() . "</P>");
}
?>
ASSIGNMENT

Put a link labeled "Delete this Joke" next to


each joke on the page that, when clicked,
will remove that joke from the database
and display the updated joke list.
SOLUTION
<HTML>
<BODY>
<FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=POST>
<P>Type your joke here:<BR>
<TEXTAREA NAME="joketext" ROWS=10 COLS=40 WRAP>
</TEXTAREA><BR>
<INPUT TYPE=SUBMIT NAME="submitjoke" VALUE="SUBMIT">
</FORM>
<?php
// Connect to the database server
$dbcnx = @mysql_connect(
"localhost", "root", "mypasswd");
if (!$dbcnx) {
echo( "<P>Unable to connect to the " .
"database server at this time.</P>" );
exit();
}
// Select the jokes database
if (! @mysql_select_db("jokes") ) {
echo( "<P>Unable to locate the joke " .
"database at this time.</P>" );
exit();
}
echo("<P> Here are all the jokes " .
"in our database: </P>");
$result = mysql_query(
"SELECT ID, JokeText FROM Jokes");
if (!$result) {
echo("<P>Error performing query: " .
mysql_error() . "</P>");
exit();
}
while ( $row = mysql_fetch_array($result) ) {
$jokeid = $row["ID"];
$joketext = $row["JokeText"];
echo("<P>$joketext " .
"<A HREF='$PHP_SELF?deletejoke=$jokeid'>" .
"Delete this Joke</A></P>");
}
echo("<P><A HREF='$PHP_SELF?addjoke=1'>" .
"Add a Joke!</A></P>");
endif;
?>
</BODY>
</HTML>
if ("SUBMIT" == $_POST['submitjoke']) {
$sql = "INSERT INTO Jokes SET " .
"JokeText='$joketext', " .
"JokeDate=CURDATE()";
if (mysql_query($sql)) {
echo("<P>Your joke has been added.</P>");
} else {
echo("<P>Error adding submitted joke: " .
mysql_error() . "</P>");
}

if (isset($_POST['deletejoke'])) {
$sql = "DELETE FROM Jokes " .
"WHERE ID=$deletejoke";
if (mysql_query($sql)) {
echo("<P>The joke has been deleted.</P>");
} else {
echo("<P>Error deleting joke: " .
mysql_error() . "</P>");
}
}
SESSIONS

Session support in PHP consists of a way to preserve certain


data across subsequent accesses. This enables you to build
more customized applications and increase the appeal of
your web site.

A visitor accessing your web site is assigned a unique id,


the so-called session id. This is either stored in a cookie on
the user side or is propagated in the URL.
SESSION FUNCTIONS

session_start() - creates a session or resumes the current


one based on the current session id that's being passed via
a request, such as GET, POST, or a cookie.

string session_id ([ string $id ] ) - Get and/or set the


current session id

session_regenerate_id ([ bool $delete_old_session ] )-


Update the current session id with a newly generated one
EXAMPLE
<?php
session_start();
sessiion_regenerate_id();
if (empty($_SESSION['count'])) {
$_SESSION['count'] = 1;
} else {
$_SESSION['count']++;
}
?>
<p>
Hello visitor, you have seen this page <?php echo $_SESSION['count'];
?> times.
</p>

<p>
To continue, <a href="nextpage.php?<?php echo
htmlspecialchars(session_id()); ?>">click
here</a>.
</p>
SESSION LOGIN
<?php
session_start();
$_SESSION['active']=false;
$_SESSION['uname']=false;
if (! $_SESSION[active]) {
$_SESSION[active] = 1;
$heading = "Welcome - please log in";
$htmlsay = "Please enter your name <input name=whoareyou><input
type=submit>";

}
/*
else
{
if (! $_SESSION['uname'] && ($_POST['whoareyou'])) {
$_SESSION['uname'] = $_POST['whoareyou'];

}
}
*/
?>
<html>
<head></head>
<body>
<form method=post action="<?php echo $PHP_SELF;?>">
<?php echo $htmlsay;?>
</form>
</body>
</html>
<?php
if(isset($_SESSION['active']))
{
if(isset($_POST['whoareyou'])
{
$_SESSION['uname']=$_POST['whoareyou'];
print “<a href='welcome.php'>Continue</a>”;
}
}
?>
<html>
<head></head>
<body>
<?php
sssion_start()
IF($_SESSION['uname']==”baskar”)
{
print “Welcome Mr.”.$_SESSION['uname'];
}
else
{
print “You are not authorised to view this page”;
}
?>
</body>
</html>
HTTP

HTTP operates over TCP connections, usually to port 80,


though this can be overridden and another port used. After
a successful connection, the client transmits a request
message to the server, which sends a reply message back.
HTTP messages are human-readable, and an HTTP server
can be manually operated with a command such as telnet
server 80

The simplest HTTP message is "GET url", to which the


server replies by sending the named document. If the
document doesn't exist, the server will probably send an
HTML-encoded message stating this.
HTTP HEADERS

GET / HTTP/1.0 >


>
< HTTP/1.0 200 OK
< Date: Wed, 18 Sep 1996 20:18:59 GMT
< Server: Apache/1.0.0
< Content-type: text/html
< Content-length: 1579
< Last-modified: Mon, 22 Jul 1996 22:23:34 GMT
<
< HTML document
In addition to GET requests, clients can also send
HEAD and POST requests, of which POSTs are the
most important.

POSTs are used for HTML forms and other operations


that require the client to transmit a block of data to
the server.

After sending the header and the blank line, the client
transmits the data. The header must have included a
Content-Length: field, which permits the server to
determine when all the data has been received.
INSTALLING APACHE
Windows
1. Download XAMPP from apachefriends.org
2. unzip it under C:\
Linux
1. All recent distributions comes with apache.
2. Choose Web Server under Server while installing
OR
3. After installing linux, go to Add/Remove choose apache and install
OR
4. Download the tarball from apache.org and install using the following
steps
unzip and untar the tarball. You should get a new directory with the
name apache-2.x under the present directory.
Descend to the new directory and type
configure
make
as a root type 'make install'
CONFIGURING APACHE

The main configuration file for apache is httpd.conf


Windows
C:\xampp\httpd.conf
Linux
/etc/httpd/httpd.conf
/etc/apache2/apache2.conf
VIRTUAL HOST
Debian based distribution
create a file example.com.conf under /etc/apache2/sites-
available.
<VirtualHost dev.example.com>
ServerAdmin webmaster@localhost
ServerAlias www.dev.example.com
DocumentRoot /home/myuser/public_html/example.com
#if using awstats
ScriptAlias /awstats/ /usr/lib/cgi-bin/
</VirtualHost>
under /etc/apache2/sites-enabled create a link to the file we
just created:

cd /etc/apache2/sites-enabled
ln -s /etc/apache2/sites-available/example.com.conf
example.com.conf
In other distributions
Simply add the following lines to httpd.conf
NameVirtualHost *

<VirtualHost *>
ServerName www.domain.tld
DocumentRoot /www/domain
ServerAlias domain.tld *.domain.tld
</VirtualHost>

Listen 10.1.2.3:80
Listen 10.1.2.4:8000

<VirtualHost 10.1.2.3>
DocumentRoot /www/vhost1
ServerName www.my-dom.com
</VirtualHost>
MONITORING

<Location /server-status>
SetHandler server-status
Order deny,allow
Deny from all
Allow from 127.0.0.1
</Location>
HTTP://127.0.0.1/server-status
You could be using localhost instead of 127.0.0.1, but keep
in mind that if the directive HostnameLookups is turned to
Off, apache won't let you access to
http://localhost/server-status
THANK YOU
By
BASKAR. K
9940024492
baskar_k@sify.com