Tips

Tips

Video

Showing posts with label Source code. Show all posts
Showing posts with label Source code. Show all posts

Saturday, October 24, 2015

Get current link path of website from PHP

Get current link path of website from PHP









This function will be help you how to get current link path of website from PHP

Source code


<?php
  function getCurrentPageURL() {
    $pageURL = 'http';
 
    if (!empty($_SERVER['HTTPS'])) {
      if ($_SERVER['HTTPS'] == 'on') {
        $pageURL .= "s";
      }
    }
 
    $pageURL .= "://";
 
    if ($_SERVER["SERVER_PORT"] != "80") {
      $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] 
                                                . $_SERVER["REQUEST_URI"];
    } else {
      $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
    }
 
    return $pageURL;
  }
?>

Description above function:

Many people have many surprise and don't understand about function and variables
$_SERVER[“SERVER_NAME”], $_SERVER[“SERVER_PORT”]

Please read below comment, you'll be easy to understand it.
Ex I have one link patch same as:


http://sinhvienit.net/@forum/showthread.php?t=2053
http://learn-tech-tips.blogspot.com/2015/10/create-picture-with-light-border.html


When you use $SERVER, we have get some result here

$_SERVER[‘HTTP_HOST’] =>
learn-tech-tips.blogspot.com 

 $_SERVER[‘PHP_SELF’] => 
/2015/10/create-picture-with-light-border.html

$_SERVER[‘REQUEST_TIME’] => Time which client requested - TIME _STAMP

$_SERVER[‘QUERY_STRING’] => t = 2053???

$_SERVER[‘DOCUMENT_ROOT’] => folder contain source code
Ex: /home/learn-tech-tips/public_html

$_SERVER[‘REMOTE_HOST’] = Hostname of user access
$_SERVER[‘REMOTE_PORT’] => Remote Port of Browser connect to server

$_SERVER[‘REQUEST_URI’] =>
2015/10/create-picture-with-light-border.html

$_SERVER[‘SERVER_NAME’] => Name of server (same computer name)
Ex: learn-tech-tips Server

$_SERVER[‘SERVER_ADDR’] => IP of server

$_SERVER[‘REMOTE_ADDR’] => IP of access person

$_SERVER[‘HTTP_USER_AGENT’] => Information with browser of access person










Have a lucky day!
Zidane



Saturday, July 25, 2015

Bài 2: Kiểm tra Ma Trận cuông sắp tăng dần theo đường chéo chính!





Ví dụ:
Ma trận tăng dần theo đường chéo chính như sau
11.00 16.00  20.00  23.00  25.00
47.00 12.00  147.00 21.00 24.00 
1.00  50.00   13.00  15.00  0
1.00  50.00   13.00  14.00  100
1.00  460.00 13.00  15.00   15.00 
Ý tưởng: 
Giống như cách sắp xếp trên mảng 1 chiều, ta xem đường chéo chính như là mảng một chiều sau đó kiểm tra nó có tăng dần không, nếu tăng dần trả về 1, ngược lại bằng 0

Cách làm:
Viết hai hàm: 
- Một hàm dùng để kiểm tra tăng dần (ktTangDan)
- Một hàm xuất kết quả

Ghi Chú:
- Các hàm nhập, xuất tự viết!


Chương trình




/* ------------------------------------------
 * Author: zidane (huuvi168@gmail.com)
 * Last modified: 2015-07-03
 * -----------------------------------------/

// n số phần tử của mảng a
int ktTangDan(float a[], int n) 
{
    bool flag = true;
    for (int i=0; i<=n-1; i++)
       for (int j=i+1; j<=n; i++)
         // chỉ cần một phần tử thỏa là không tăng dần
         if (a[i] > a[j]) 
            // giá trị trả về trong hàm function (pascal)
            flag = false; 
     return flag;
}

// n, m là dòng và cột trong ma trận (ma trận vuông)
void ktTangDanDuongCheoChinh(float a[][100], int n)
{
   float b[100];
   for (int i=0; i<n; i++)
   b[i] = a[i][i];

   if (ktTangDan(b, n) == true)
      printf("Duong cheo chinh tang dan");
   else
      printf("Duong cheo chinh KHONG tang dan");
}




Sunday, July 19, 2015

Bài 1: Tìm khoảng cách nhỏ nhất từ các phần tử của mảng đến x bất kỳ

Cách giải
Lưu ý

- Phần tử đầu tiên của Mảng trong pascal đi từ vị trí số 1
- Phần tử đầu tiên của Mảng trong C++ đi từ vị trí số 0
Bên dưới là cách giải dùng C++
/* ------------------------------------------
 * Author: zidane (huuvi168@gmail.com)
 * Last modified: 2015-07-03
 * -----------------------------------------/

#include "stdafx.h"
#include <stdlib.h>
#include <conio.h>

// ----------------------------------------
int getMin(int a[], int n)
{
   int min = a[0];

   for (int i = 0; i < n; i++)
   if (a[i] < min)
       min = a[i];

   return min;
}

// ----------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
   int a[10];
   int n = 10;

   for (int i=0; i < n; i++)
   {
      a[i] = (i+1) * 10;
      printf ("%d ", a[i] );
   }

   int b[10];

   int x = 100; 
   int k = 0;

   for (int i=0; i < n; i++)
   {  
      b[k] = abs (a[i] - x);  // abs: hàm lấy giá trị tuyệt đối
      k = k + 1;
   }
 
   printf ("Gia tri nho nhat: %d" , getMin(b, k));
   _getch();
   return 0;
}