技術情報
- 2019年11月15日
- 技術情報
Session & Cookie
Session Information Resides on the Web Server
A session is server-side information intended to exist only throughout the visitor’s interaction with the website. Only a unique identifier is stored on the client side. This token is passed to the web server when the visitor’s browser requests your HTTP address. That token matches your website with the visitor’s information while the user is at your site. When the user closes the website, the session ends, and your website loses access to the information. If you don’t need any permanent data, sessions are usually the way to go. They are a little easier to use, and they can be as large as needed, in comparison with cookies, which are relatively small.
Sessions cannot be disabled or edited by the visitor.
So, if you have a site requiring a login, that information is better served as a cookie, or the user would be forced to log in every time he visits. If you prefer tighter security and the ability to control the data and when it expires, sessions work best.
You can, of course, get the best of both worlds. When you know what each does, you can use a combination of cookies and sessions to make your site work exactly the way you want it to work.
Session usage examples
//session global variable
$_SESSION
//session start
session_start();
//storing session
$_SESSION["username"] = "admin";
$_SESSION["name"] = "yuuma";
$_SESSION["dept"] = "web";
//retrieving session
echo 'Welcome:' . $_SESSION["username"] . '<br>';
echo 'Your name is ' . $_SESSION["name"] . '<br>';
echo 'Your department is ' . $_SESSION["dept"];
//the output will be look like this.
Welcome:admin
Your name is yuuma
Your department is web
// remove all session variables
session_unset();
// destroy the session
session_destroy();
A Cookie Resides on the User’s Computer
Your website can be set to place a cookie on a user’s computer. That cookie maintains information in the user’s machine until the information is deleted by the user. A person may have a username and password to your website. That information can be saved as a cookie on the visitor’s computer, so there is no need for him to log in to your website on each visit. Common uses for cookies include authentication, storage of site preferences, and shopping cart items. Although you can store almost any text in a browser cookie, a user can block cookies or delete them at any time. If, for example, your website’s shopping cart utilizes cookies, shoppers who block cookies in their browsers can’t shop at your website.
Cookies can be disabled or edited by the visitor. Do not use cookies to store sensitive data.
Cookie usage examples
//setting cookie
setcookie(name, value, expire, path, domain, security,httponly);
Name– The name of the cookie. It’s mandatory.The server will use when retrieving its value from the $_COOKIE array variable.
Value –The setcookie() function set the value of the name variable and It contains that you actually want to store. It’s also mandatory.
Expiry- This is a limit to access a cookie. The time may be 1 day ,1 month or more. If you set the time 1 month then you can access the cookie during the 1-month duration. After one month you can not access the cookie.
Path- Path specifies the directories from which the cookie is valid .
Domain – This can be used to specify the domain name in very large domains and must contain at least two periods to be valid. All cookies are only valid for the host and domain which created them.
Security- It set to be 1 refers that the cookie should only be sent by secure transmission using HTTPS and else set to 0 which means cookie can be sent by regular HTTP.
httponly If it is set to true, then only client side scripting languages .for example – JavaScript cannot access them. Set the cookie username, mobile, and email. The cookie will expire after one hour in the below example.
setcookie("username", "john", time()+3600, "/","", 0);
setcookie("email", "john@example.com", time()+3600, "/", "", 0);
//retrieving cookie
$_COOKIE['cookiename']
//removing cookie
//To delete a cookie you need to use setcookie() function as well as with its name. You can destroy cookies before the expiry time. You need to define your given expiry time. Let's have a look. The PHP code help destroy the cookies before the expiry time.
setcookie("admin", "", time() - 3600,'/');
By Yuuma
yuuma at 2019年11月15日 10:00:02
- 2019年11月08日
- 技術情報
Synchronous VS Asynchronous
If two processes are running Synchronously, it means that second process will wait until or unless first process complete.
If two processes are running Asynchronously, it means second process will not wait for first process result and will be executed.
I will explain a bit more detail in below for each communication.
Synchronous Communication
If two processes are running Synchronously
, it means that second process will wait until or unless first process complete. Synchronous Transmission is efficient, reliable and is used for transferring a large amount of data. It provides real-time communication between connected devices. Chat Rooms, Video Conferencing, telephonic conversations, as well as face to face interactions, are some of the examples of Synchronous Transmission.
Check the sample php snippet below for synchronous communication.
<?php
echo(“Before the first file is read.”);
$fileContents = file_get_contents(“sample.txt”);
//You can’t do anything while the file is being read, your script is ‘stuck’.
echo(“After the first file has completed reading.”);
echo(“Before the second file is read.”);
$fileContents = file_get_contents(“sample2.txt”);
//You can’t do anything while the file is being read, your script is ‘stuck’.
echo(“After the second file has completed reading.”);
==========================
Output always looks like this:
Before the first file is read.
After the first file has completed reading.
Before the second file is read.
After the second file has completed reading.
Asynchronous Communication
If two processes are running Asynchronously
, it means second process will not wait for second process result and will be executed. It is simple, fast, economical and does not require a 2-way communication. Letters, emails, forums, televisions and radios are some of the examples of Asynchronous Transmission.
Check the js snippet below for asynchronous communication
console.log(“Before the first file is read.”);
hypotheticalFileGetContents(“sample.txt”, function(fileContents){
//fileContents now contains the file contents, this function is only called when the file read in the background has finished
console.log(“After the first file has completed reading.”);
});
//You’ve now told it to start the first read, but it won’t ‘block’ your script execution. It will do the read in the background, and immediately move on with the rest of your code.
console.log(“Before the second file is read.”);
hypotheticalFileGetContents(“sample2.txt”, function(fileContents){
//fileContents now contains the file contents, this function is only called when the file read in the background has finished
console.log(“After the second file has completed reading.”);
});
=======================
Output could look something like this:
Before the first file is read.
Before the second file is read.
After the first file has completed reading.
After the second file has completed reading.
By Yuuma
yuuma at 2019年11月08日 10:09:22
- 2018年06月28日
- 技術情報
HTMLCollectionがiterableかどうかの検証
突然ですが、Chrome、Safari、Firefox、Microsoft Edgeの中で
以下のコードが動かないものが一つだけあります。
<script> |
yoshimoto at 2018年06月28日 10:00:15
PHP5とPHP7の共存 on Windows 10
今回は、研修中に偶然私のPC(Windows 10)上で出来上がったPHP5とPHP7の共存方法をご紹介させていただきます。
以下のようにコマンドプロンプト上において、php とコマンドを打てばPHP7が、php5 とコマンドを打てばPHP5が起動するようにしました。
これによってPHP5では動作するけれど、PHP7では動作しないといったコードの検証を素早く行うことが出来ます。
続きを読む
yoshimoto at 2018年05月17日 10:10:09
- 2018年05月10日
- 技術情報
エンジニアに捧げる「Bootstrapを使っての入力フォームの体系的な作成法」2
前回は、FormGroupを使って縦の入力フォームの作り方を紹介させていただきました。
今回は、グリッドレイアウトシステムを使ってFormGroupを横並びに配置する方法を紹介させていただきます。
グリッドレイアウトシステムは、rowやcolumnを使って画面を分割しながらレイアウトが行うことができるCSSの機能の一つです。
Bootstrapでは、form-rowというクラスを使用することで、FormGroupを横並びに配置することが出来ます。
yoshimoto at 2018年05月10日 10:00:36