Google Ads

Category: PHP Code


Auto Redirect HTTPS to full site – PHP Code

 

  • Find out header,php file from your script
  • and copy following codes
  • Past codes to very top in header,php

 

 
 
  1. <?php
  2. if (!(isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' ||
  3. $_SERVER['HTTPS'] == 1) ||
  4. isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
  5. $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'))
  6. {
  7. $redirect = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  8. header('HTTP/1.1 301 Moved Permanently');
  9. header('Location: ' . $redirect);
  10. exit();
  11. }
  12. ?>

 

Try Your Self ->

 


Redirecting from HTTP to HTTPS with PHP on IIS – PHP Code

 

 
 
  1. <?php
  2. if (! isset($_SERVER['HTTPS']) or $_SERVER['HTTPS'] == 'off' ) {
  3. $redirect_url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  4. header("Location: $redirect_url");
  5. exit();
  6. }
  7. ?>

 

Try Your Self ->

 


Auto Redirecting from HTTP to HTTPS with PHP – PHP Cod

Open your index.php file and copy following codes and post in index.php on very top.

 

 
 
  1. <?php
  2. if (!(isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' ||
  3. $_SERVER['HTTPS'] == 1) ||
  4. isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
  5. $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'))
  6. {
  7. $redirect = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  8. header('HTTP/1.1 301 Moved Permanently');
  9. header('Location: ' . $redirect);
  10. exit();
  11. }
  12. ?>

 

Try Your Self ->

 


Horizontal/Vertical table headings HTML Tables HTML Examples – HTML Code

 

 
 
  1. <style>
  2. table, th, td {
  3. border: 1px solid black;
  4. border-collapse: collapse;
  5. }
  6. th, td {
  7. padding: 5px;
  8. text-align: left;
  9. }
  10. </style>
  11. </head>
  12. <body>
  13. <h2>Horizontal Headings:</h2>
  14. <table style="width:100%">
  15. <tr>
  16. <th>Name</th>
  17. <th>Telephone</th>
  18. <th>Telephone</th>
  19. </tr>
  20. <tr>
  21. <td>Bill Gates</td>
  22. <td>555 77 854</td>
  23. <td>555 77 855</td>
  24. </tr>
  25. </table>
  26. <h2>Vertical Headings:</h2>
  27. <table style="width:100%">
  28. <tr>
  29. <th>Name:</th>
  30. <td>Bill Gates</td>
  31. </tr>
  32. <tr>
  33. <th>Telephone:</th>
  34. <td>555 77 854</td>
  35. </tr>
  36. <tr>
  37. <th>Telephone:</th>
  38. <td>555 77 855</td>
  39. </tr>
  40. </table>

 

 


PHP User online – PHP Code

 

In this tutorial, create 1 PHP file for testing this code.

 user_online.php

Steps

Create table “user_online” in mysql in database “test”.

Create file user_online.php.

 

STEP 1: Create table “user_online”

 
 
  1. CREATE TABLE `user_online` (
  2. `session` char(100) NOT NULL default '',
  3. `time` int(11) NOT NULL default '0'
  4. ) TYPE=MyISAM;

 

STEP2: Create file – user_online.php

 
 
  1. <?php
  2. session_start();
  3. $session=session_id();
  4. $time=time();
  5. $time_check=$time-600; //SET TIME 10 Minute
  6. $host="localhost"; // Host name
  7. $username=""; // Mysql username
  8. $password=""; // Mysql password
  9. $db_name="test"; // Database name
  10. $tbl_name="user_online"; // Table name
  11. // Connect to server and select databse
  12. mysql_connect("$host", "$username", "$password")or die("cannot connect to server");
  13. mysql_select_db("$db_name")or die("cannot select DB");
  14. $sql="SELECT * FROM $tbl_name WHERE session='$session'";
  15. $result=mysql_query($sql);
  16. $count=mysql_num_rows($result);
  17. if($count=="0"){
  18. $sql1="INSERT INTO $tbl_name(session, time)VALUES('$session', '$time')";
  19. $result1=mysql_query($sql1);
  20. }
  21. else {
  22. "$sql2=UPDATE $tbl_name SET time='$time' WHERE session = '$session'";
  23. $result2=mysql_query($sql2);
  24. }
  25. $sql3="SELECT * FROM $tbl_name";
  26. $result3=mysql_query($sql3);
  27. $count_user_online=mysql_num_rows($result3);
  28. echo "User online : $count_user_online ";
  29. // if over 10 minute, delete session
  30. $sql4="DELETE FROM $tbl_name WHERE time<$time_check";
  31. $result4=mysql_query($sql4);
  32. // Open multiple browser page for result
  33. // Close connection
  34. mysql_close();
  35. ?>

 

 

If you have any question then you can comment below.

 


There are options that users may set to change the browser’s default caching settings. By sending the headers above, you should override any of those settings and force the browser to not cache!

Let the user be prompted to save a generated PDF file (Content-Disposition header is used to supply a recommended filename and force the browser to display the save dialog box):

 

Direct Code:

Use this code out of <?php ?> 

<?php
header('Location: http://www.example.com/');
?>

 

Prevent page caching:

<?php
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache");
header("Pragma: no-cache");
?>

<html>
<body>

 

 


Write a PHP script to get the first word of a sentence.

Original String : ‘The quick brown fox” Even though it is little late, but PHP has one better solution for this:

Sample Solution:

Using split function also you can get the first word from string.

 

PHP Code:

 
 
  1. <?php $s = 'The quick brown fox'; $arr1 = explode(' ',trim($s)); echo $arr1[0]."\n"; ?>

or

 
 
  1. <?php echo strstr( $myvalue . ' ', ' ', true ); ?>

 


You can show in page the last word of a sentence.

It depends on what you try to do (it is hard to understand from your description) but to get the last word from a string you can do

 

How it will work?

The code will support according to your php.

 

$split = explode(" ", $string); echo $split[count($split)-1];

 

"