Skip to main content

Posts

Showing posts with the label Server Side

Create a contact form using php

Create this file ⬇ contactform.php <html> <head> </head> <body> <h4>Contact Form</h4> <form method="post "action="sendmail .php">  Name : <input type="text"name="uname"><br> Mobile No. : <input type="text" name="mobile"><br> Email id : <input type="text" name="email"><br> Message : <textarea name="message"> </textarea><br> <input type="submit" value="Submit"> </form> </body> </html> sendmail.php <?php $uname = $_POST['uname']; $mobile = $_POST['mobile']; $email = $_POST['email']; $message = $_POST['message']; $to = "phpkish@gmail.com"; $subject = "Contact"; $message = $message."\n Name: ".$uname."\n Mobile: ".$mobile."\n Email: $email; if(mail($to,$s...

Read an image from database using PHP

PHP program to Read image from Database.  <?php header("content-type:image/jpeg");     mysql_connect("servername","username","password"); mysql_select_db("databasename");  $sql = "select * from tablename"; $data = mysql_query($sql);     while($rec=mysql_fetch_row($data))    {      echo "$rec[1]<br>";    }  ?>  ------------------------------------------------------------- Note: When we using XAMPP or WAMP, servername = localhost, username = root, password is empty.

Connect to server and select database using php

<?php if(!mysql_connect('servername','username','password'))  {      die('Connection failed!'.mysql_error());  }       if(!mysql_select_db("dbname"))  {  die('Database unavailable'.mysql_error());  }  ?> ------------------------------------------------------------- Note: When we are using XAMPP or WAMP, servername = localhost, username = root, password is empty

Create a database with PHP and Mysql

<?php  if(! mysql_connect('servername','username','password'))  {     die('Connection failed!'.mysql_error());  } $sql = "create database newdb"; if(mysql_query($sql)) {  echo "Database created"; } else {   echo mysql_error(); } ?> ------------------------------------------------------------- Note: When we are using XAMPP or WAMP, servername = localhost, username = root, password is empty.

PHP file upload

Program to Upload a file to the Server. ✡️ form1.php  <html>  <head>  </head>  <body> <form method="post"   action="upload.php" enctype="multipart/     form-data">  Resume : <input type="file" name="f1"> <br> <input type="submit" value="Upload"> </form> </body> </html> ✡️ Upload.php <?php if(is_uploaded_file($_FILES['f1']['tmp_name'])) {  $fname = $_FILES['f1']['name'];            if(move_uploaded_file($_FILES['f1']['tmp_name'],"uploads/$fname"))  {  echo "Uploaded successfully";  } else  {  echo "Not uploaded";  }  } ?>

PHP Login and Logout example using sessions.

Program to create simple Login and Logout example using sessions. ✡️login.php  <html>  <head>  <title>Login Form</title>  </head>  <body>  <h2>Login Form</h2>  <form method="post"   action="checklogin.php"> User Id: <input type="text" name="uid"> <br> Password: <input type="password" name="pw"> <br> <input type="submit" value="Login"> </form> </body> </html> ✡️ checklogin.php  <?php  $uid = $_POST['uid'];  $pw = $_POST['pw']; if($uid == 'arun' and $pw == 'arun123') {  session_start(); $_SESSION['sid']=session_id();              header("location:securepage.php");  } ?> ✡️securepage.php  <?php session_start();  if($_SESSION['sid']==session_id())  {         echo "Welcome to you<br>";  ...

PHP program to separate odd and even elements from array without using loop

<?php $input = array(4, 3, 6, 5, 8, 7, 2); function oddCmp($input) {     return ($input & 1); } function evenCmp($input) {     return !($input & 1); } $odd = array_filter($input, "oddCmp"); $even = array_filter($input, "evenCmp"); $odd = array_values(array_filter($odd)); $even = array_values(array_filter($even)); print"Odd array :\n"; print_r($odd); print"\nEven array :\n"; print_r($even); ?>

Simple PHP calculator

Let's create a simple calculator <?php  if(isset($_POST['sub']))  {   $txt1=$_POST['n1'];                       $txt2=$_POST['n2'];     $oprnd=$_POST['sub'];  if($oprnd=="+")  $res=$txt1+$txt2; else if($oprnd=="-")              $res=$txt1-$txt2; else if($oprnd=="x")        $res=$txt1*$txt2; else if($oprnd=="/")    $res=$txt1/$txt2;  } ?> <form method="post" action=""> Calculator <br> No1:<input name="n1" value="<?php echo $txt1; ?>"> <br> No2:<input name="n2" value="<?php echo $txt2; ?>"> <br> Res:<input name="res" value="<?php echo $res; ?>"> <br> <input type="submit" name="sub" value="+"> <input type="submit" name="sub" value="-"> <input type="submi...

How to get a file size in PHP

Hello guys today we are going to write a PHP program to get the size of a file. <?php $myfile = fopen("/home/students/ppp.txt", "w") or die("Unable to open file!"); $txt = "PHP Exercises\n"; fwrite($myfile, $txt); $txt = "from\n"; fwrite($myfile,$txt); $txt = "hello World\n"; fwrite($myfile, $txt); fclose($myfile); echo "Size of the file".filesize("/home/students/ppp.txt")."\n"; ?>

PHP program to list prime numbers

Here is the Program to list the first 15 prime numbers. <?php  $count = 0;  $num = 2;  while ($count < 15 )  {  $div_count=0;  for ( $i=1; $i<=$num; $i++)  {  if (($num%$i)==0)  {  $div_count++;  }  }  if ($div_count<3)  {  echo $num." , ";  $count=$count+1;  }  $num=$num+1; }  ?> 

How to swap numbers in PHP

 Swap two number using third variable program in PHP <?php $a=10; $b=20; echo "Value of a: $a</br>"; echo "Value of b: $b</br>";        $temp=$a; $a=$b; $b=$temp; echo "Value of a: $a</br>"; echo "Value of b: $b</br>"; ?> Output: Value of a: 10 Value of b: 20 Value of a: 20 Value of b: 10