sondmk header
การเขียนโปรแกรมด้วยภาษา PHP เบื้้องต้น

PHP : Random String Function , Random Password , Password Generater

Post by Goborijung at 2021-02-11 09:09:46 | ID: 619

<?php

function randomString($length = 6) {
	$str = "";
	$characters = array_merge(range('A','Z'), range('a','z'), range('0','9'));
	$max = count($characters) - 1;
	for ($i = 0; $i < $length; $i++) {
		$rand = mt_rand(0, $max);
		$str .= $characters[$rand];
	}
	return $str;
}

echo randomString();

?>

// Password Generater

<?php
function randomString($length = 6) {
	$str = "";
	$characters = array_merge(range('A','Z'), range('a','z'), range('0','9'),range(chr(33),chr(47)));
	$max = count($characters) - 1;
	for ($i = 0; $i < $length; $i++) {
		$rand = mt_rand(0, $max);
		$str .= $characters[$rand];
	}
	return $str;
}

$row = 20;
for($i=1; $i<=$row; $i++)
{
	echo randomString(10).'<br>';
}
?> 

PHP : Remember Me

Post by Goborijung at 2020-05-26 21:06:02 | ID: 585

/* ob_start and session() */
<?php
  ob_start(); //ประมวลผลคำสั่งทุกบรรทัดให้เสร็จก่อน ค่อยส่งให้ client ทีเดียว
  session_start();

  //ตรวจสอบว่ามีการเก็บ cookie ของ member เอาไว้หรือไม่ ถ้ามี ก็เอาไปแสดงในฟอร์ม
  $ck_email = isset($_COOKIE['ck_email'])?$_COOKIE['ck_email']:'';
  $ck_pwd   = isset($_COOKIE['ck_pass'])?$_COOKIE['ck_pass']:'';

  //ถ้า cookie-email ไม่ว่าง ให้ input-checkbox = checked
  if($ck_email=="")
  {
    $checked = "";
  }
  else
  {
    $checked = "checked";
  }
?>

/* html form */
<form method="post">
  <input type="email" name="email" value="<?=$ck_email?>" class="form-control" placeholder="Email">
  <input type="password" name="pass" value="<?=$ck_pwd?>" class="form-control" placeholder="Password">
  <input type="checkbox" name="keep_login" value="on" <?=$checked;?> > Remember Me
  <button type="Submit" name="btnSign" class="btn btn-primary btn-block btn-flat"><i class="fa fa-sign-in"></i> Sign In</button>
</form>

/* php do submit */
<?php
  
  if(user==user && pass==pass)
  {
    $keep_login = @$_POST['keep_login']; //echo 'keep_login: '.$keep_login; // --------------------------- (1)
    if($keep_login=='on')
    {
      //echo "<script>alert('keeped');</script>";
      setcookie('ck_email',$_POST['email'],time()+86400,'/');
      setcookie('ck_pass',$_POST['pass'],time()+86400,'/');
    }
    else
    {
      //echo "<script>alert('not keep');</script>";
      setcookie('ck_email','',time()-3600,'/');
      setcookie('ck_pass','',time()-3600,'/');
    }

    $rs = mysqli_fetch_array($qry);          
    $_SESSION['user_login_id'] = $rs['uid'];
    echo "<script>window.location.href='welcome';</script>";
  }
?>

PHP : SET Option

Post by Goborijung at 2020-03-13 11:49:57 | ID: 424

<?php
function setOption($name,$arrVal,$post)
{
	echo "<select name='".$name."' id='".$name."' class='form-control form-control-sm'>";
	foreach($arrVal as $key => $val)
	{
		if($key == @$post)
		{
			echo "<option value='".$key."' selected>".$val."</option>";
		}
		else
		{
			echo "<option value='".$key."'>".$val."</option>";
		}
	}
	echo "</select>";
} //end-function

>> การใช้งาน <<

$htmlForm = new htmlForm();
$arrBranch = array(
	'NULL'			=>	''
	,'SAI4'			=>	'SAI4'
	,'SAI4-2'		=>	'SAI4-2'
	,'304'			=>	'304'
	,'RAMA2'		=>	'RAMA2'
 	//,'3FACTORY'	=>	'3Factory'
);
$htmlForm -> setOption('branch',$arrBranch,@$branch);
?>

PHP : Show Query (Admin Only)

Post by Goborijung at 2020-08-20 08:50:25 | ID: 731

<?php
	if($_SESSION['UserDepartmentCode'] == 'Admin')
	{
		$checked = '';
		if(@$chkQuery <> ''){ $checked = 'checked'; }
		echo "<input type='checkbox' name='chkQuery' ".$checked."> ShowQuery";
	}
?>

PHP : site: ที่เกียวข้อง

Post by Goborijung at 2018-12-24 17:03:53 | ID: 74

www.reviva.co.th/store/

PHP : str_replace() , replace

Post by Goborijung at 2022-03-14 16:50:15 | ID: 1413

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

PHP : Substr (Substring)

Post by Goborijung at 2021-03-08 13:00:56 | ID: 426

>> PHP

ตัดจากฝั่งซ้าย ตั้งแต่ Index ที่ 4 เป็นต้นไป

<?php echo substr("Hello world",4); // Output : o world ?>

ตัดจากฝั่งซ้าย เริ่มจาก Index ที่ 1 เอามา 2 ตัว

echo substr("Hello world",1,2); // Output : el

ตัดจากฝั่งขวา เอา 5 ตัวสุดท้าย

// Substring Right echo substr("Hello world",-5)."<br>"; //output = world

ตัดจากฝั่งขวา เอา 5 ตัวสุดท้าย และเอามา 3 ตัว

echo substr("Hello world",-5,3); //output: wor -- เริ่มจาก index 0 และ ตัดมา 28 ตัว <?php echo mb_substr($rs['QCRemark1'],0,28); ?> >> Javascript <script> var str = "Hello world!"; var res = str.substring(1, 4); </script>

PHP : switch case

Post by Goborijung at 2022-06-03 14:46:55 | ID: 1583

<?php
$favcolor = "red";

switch ($favcolor) {
  case "red":
    echo "Your favorite color is red!";
    break;
  case "blue":
    echo "Your favorite color is blue!";
    break;
  case "green":
    echo "Your favorite color is green!";
    break;
  default:
    echo "Your favorite color is neither red, blue, nor green!";
}
?>



PHP : This Page , $thisPage = $_SERVER["PHP_SELF"];

Post by Goborijung at 2021-03-19 11:35:10 | ID: 1054

$thisPage = $_SERVER["PHP_SELF"];

Example : 
<button class="btn btn-sm btn-danger" type="button" name="reset" onclick="window.location.href='<?=$thisPage?>' "><i class="fa fa-times"></i> Reset</button>

PHP : Time , DateTime , Function Date Time

Post by Goborijung at 2021-08-16 08:35:35 | ID: 1316

<?php
                    
  function diff2time($time_a,$time_b)
  {
    $now_time1    = strtotime(date("Y-m-d ".$time_a));
    $now_time2    = strtotime(date("Y-m-d ".$time_b));
    $time_diff    = abs($now_time2-$now_time1);
    $time_diff_h  = floor($time_diff/3600);       // จำนวนชั่วโมงที่ต่างกัน
    $time_diff_m  = floor(($time_diff%3600)/60);  // จำวนวนนาทีที่ต่างกัน
    $time_diff_s  = ($time_diff%3600)%60;         // จำนวนวินาทีที่ต่างกัน
    //return $time_diff_h." ชั่วโมง ".$time_diff_m." นาที ".$time_diff_s." วินาที";
    return $time_diff_h.".".$time_diff_m.".".$time_diff_s."h";
  }

  $curTime      = substr(date('Y-m-d H:i:s'),11,8);
  $cDate        = substr($rs['Date'],11,8);
  $closeDate    = substr($rs['closeDate'],11,8);
  $workingTime  = $rs['closeDate'] == "" ? diff2time($cDate,$curTime) : diff2time($cDate,$closeDate);

?>

<<<...3456789101112>>>

Framework

Library


เครื่องมือพัฒนาเว็บ



การออกแบบและพัฒนาเว็บไซต์


Download SourceCode



copyAllright © 2016 soundmk.com