https://github.com/php/php-src
project part 2 github link : https://github.com/rakamlarustune/The-Video-Boardcast-Plaform/tree/master/part-2
What Will I Learn?
- Each user id-key generation.
- Anonymous information for video collection.
- The video statistics in the form of the Database of save.
- To set up Php database.
- The Database relationship management.
- Analysis of Json files.
- Basic database connection with MySQL
Requirement:
- Know: General Information of php,javaScript,html5...
- Tool : Google Chrome browser (https://www.google.com/intl/tr/chrome/)
- Editor : https://notepad-plus-plus.org/download/v7.5.6.html Notepad++ ( )
- Firefox-developer version: (https://www.mozilla.org/tr/firefox/developer/)
- Localhost php software-win,app (Mac)
(Https://laragon.org/download/index.html)
Usually php versions 7.2, 7.0 is also no-plugin, no-phpframework require.
FrontEnd Development:
JavaScript jQuery CSS HTML,5,3, Bootstrap
BackEnd Development:
PHP, MySQL (MySQLi)
Difficulty
Intermediate
Tutorial Contents
We will describe here is; aggregate any user information about the video. We will continue using this data in subsequent lessons. There is no membership feature at the moment, but the user will be named with the key that will be created for the user. After that, if the user is a member and a user who is logged in, it will be collected with the user name. Non-members will be anonymous. An automatic database will be created for each video and the data will be recorded.
In the next lessons, for example, comments will be added. For such complex things, you need to install database systems first. Then, according to each video name, the recordings will be made appropriately. It will also be done by the Json filing method. Logical comparisons with Json and update techniques can be absolutely challenging. We will improve our ability to process arrays and operations with Array data types. This is how many modern software works here. Follow it from below.
Step:1
We open the Laragon program and the remaining section of the previous lesson we continue.
Here, first we need to find and generate a key value for the user. The user will be represented by a private key generated when the user enters each page. The information produced will be used frequently.
Add the "GetIP" function below as the first stage. It will take the ip value for the definition of "CLIENT". Sometimes there is an algorithm that is set up specifically to be faulty or compatible on devices such as different browsers, different phones. It has been tested on many devices.
Function getip(){
//Generating ip-CLIENT_IP or REMOTE_ADDR stuff
$ip="0";
If(isset($_SERVER['HTTP_CLIENT_IP') && isset($_SERVER['HTTP_X_FORWARDED_FOR') && isset($_SERVER_ADDR['remote')){
If (!empty($_SERVER['HTTP_CLIENT_IP')){
$ip=$_SERVER['HTTP_CLIENT_IP');
} Elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR')){
$ip=$_SERVER['HTTP_X_FORWARDED_FOR');
} Else{
$ip=$_SERVER_ADDR['remote');
}
} Else{$ip="0";}
Return $ip;
//Return value result
}
The code above the top of the “index.php” you must add a. the following code then add and continue. It also can be used for security purposes.
Function key_gen(){
$e="";
If(isset($_SERVER['HTTP_USER_AGENT')){
If(!empty($_SERVER['HTTP_USER_AGENT')){
$e=$_SERVER['HTTP_USER_AGENT');
} else{$e="0";}
}else{$e="0";}
Return sha1(getip().$e);
}
//Generate-new
$ user_id = key_gen();
If the function is based on the above key works as a manufacturer. Examples of small but function produces consistent results. You have the able to produce your own value.
Here we generate a new key with "$user_id". What we write here is what we need.
So it can be changed according to your needs in the future. Let's go to the next step
Step:2
Perform the following encoding of Javascript code "index.php" body contain add to bottom.
If you add will work properly at the bottom. But it must also be within the body.
<script>
With user_id="";
</script>
The data generated in the PHP section above has been passed to the HTML section. So there has been a change between the back and the front part. Data transfer from the rear to the front. In the section of PHP produced by the back section, users cannot see and know the PHP part. However, users can see the data that is written or produced in the JavaScript section or in the HTML section.
Try to "index.php" page in your web browser and try reflesh.
Now we have to generate the key code for the current monitored video in the same way. It will be used for many operations.
The following code index.php code must be added to the section of the upper section of the php.
<?php
$file='demo.mp4';
// Getting video hash data
$file_id=MD5($file);
With md5, it performs the ideal encryption. Key values will be required for databases too. It manufactures current information here. User key, monitored video key, and these two will be provided to record the relevant parts of the database.
Let's add it to the Javascript section of the index. PHP file again below. And the latest version is as follows. Javascript part:
<script>
With user_id="";
With file_id="";
If(user_id && file_id){
//Jquery ajax function
$.ajax({
Type:"GET"
, Url:"go.php"
, Data:{in ata_send_1':'1','v1':user_id, v2': file_id}
});
}
</script>
Above is the most important feature in the standard Jquery library. With Ajax, queries and requests can be sent and interacted. The Get or Post method is available.
Query data structure:
- Data_send_1 = 1
- V1 = user id
- V2 = file_id
Step:3
If our Go.php file is not available we will create a new go.php file. This will do most of the current operations here. Operations and data handling features will be collected in this section. Complex operations or database operations take place here.
Some of the functions of the small first assemble:
Let's write them at the top first. Security setting and session information-processing functions-library.
Go.php:
<?
$time=time() * + (86400 30);
// **PREVENTING SESSION HIJACKING**
// Prevents javascript XSS attacks aimed to steel the session ID
Ini_set('session.cookies_httponly', 1);
// **PREVENTING SESSION FIXATION**
// Session ID cannot be passed through URLs
Ini_set('session.use_only_cookies', 1);
// Uses a secure connection (HTTPS) if possible
Ini_set('session.cookies_secure', 1);
Function strtohex($string){$hex = '';for($i=0; $i<strlen($string); $i++){$oard = oard($string[$i);$hexCode = dechex($oard);$hex .= substr('0'.$hexCode, -2);}return strToUpper($hex);}
Function hextostr($hex){$string='';for($i=0; $i < strlen($hex)-1; $i+=2){ $string .= chr(hexdec($hex[$i].$hex[$i+1));}return $string;}
Function post_data($e){if(isset($_POST[$e)){return $_POST[$e);}else{return "";}}
Function get_data($e){if(isset($_GET[$e)){return $_GET[$e);}else{return "";}}
Add log_function($f,$a){$myfile = fopen($f.'.txt', the "a+") or die( Is Unable to open file!');fwrite($myfile,$a);fclose myfile($);}
Function ref_go_post($e='1'){if(isset($_POST["EBD")){$ref=$_POST["EBD");header("Location: index.php?status=$e&d=info&ref=$ref");}}
Function ref_go($e='1'){if(isset($_GET["EBD")){$ref=$_GET["EBD");header("Location: index.php?status=$e&d=info&ref=$ref");}}
Function tmp_http_eror(){error_reporting(E_ALL);header('HTTP', true, 500);}
In the session_function($a,$time){
Setcookie("cook_logind","1", time()+$time,'/');
Setcookie("cook_loginad",base64_encode($a), time()+$time,'/');
}
Cookies_reset function(){
Global $cookies_period;
If(isset ($['HTTP_SERVER_COOKIES')){
$cookies = explode(';' the $['HTTP_SERVER_COOKIES');
Foreach($cookies as $cookies){
$parts = explode('=', $cookies);
If($parts){
$name = trim($parts[0]);
If($name){
If($name=='cook_rem');}{continue
Setcookie($name,'',time()-$time);
Setcookie($name,'',time()-$time, '/');
}
}
}
}
Echo "window.location.href='index.php';</script>";exit;
}
Details:
- Cookies_reset() - It deletes everything as the session information and directs it to “index.php”.
- session_in() - Generic login parameter-template
- log_ekle() - txt log recording function
- Get_data() - The data that comes with the "Get" method will be pulled if there is none, or empty.
- Post_data() - Adds data with the "Post " method, or is empty.
- Strtohex() - converts the hex coding / numeric value
- Hextostr() - Hex encoding makes string text.
$time=time() * + (86400 30);
The session expiration time is stated above as "time". If the session is forgotten, it will automatically close itself at the end of that time. The session expires.
Step:4
Back to go.php file edecegiz continue.
We'll continue with the "go.php " file again.
Before the database connection, let's take a look at the response to the incoming "data_send_1" request. Let's look at it as a test. Add the following code by continuing.
Go.php:
$query="data_send_1";
If(isset($_GET[$query)){if($_GET[$query]=='1'){
$v1=get_data("v1");
$v2=get_data("v2");
Echo $v1."--".$v2;
}}
We can try and print the incoming parameters
If you see this here, it shows that you are working properly. However, if you are not receiving an alert, there may be an error. See if you are writing errors in the JavaScript console by switching to developer mode with the browser. Examine the specified part of the error. There may be an error in the “go.php” file.
If it's okay at this stage. We going continue. In the "Index.php" section: You can remove "alert" in the JavaScript section.
Adjust the "Success" section as shown in the following illustration. Change to it blank.
$.ajax({
Type:"GET"
, Url:"go.php"
, Data:{in ata_send_1':'1','v1':user_id, v2':file_id}
, Success:function(e){ }
});
Code detail:
“ Success : function(){ } ”
This section will be activated if the request or shipment is successful. Data that comes with the "E" parameter can be processed according to what happened. It will need to be repeated in later subjects and courses. Example: Data can be a Json text file that comes with "success". "JSON. Parse" can be done. The process can be done by turning array with Jso
type:”GET”
php data send method. Post or Get is able of to php
Url: ”go.php”
That parameter the processing php file url. Set-up “/go.php?”
Step:5
Create and set up the database in the "go.php" section.
Require_once in the config.php";
The above line will prepare the config file and import it.
Config.php file into the create and add:
<?php
// Setting from laragon database
$usr_root='root'; //root name
$usr_pass='123'; //root password
$h_name='localhost'; //always localhost able
$dbnames="dbe2"; //database name
?>
If you do not know the above information. Open Laragon and click "Database", you can access the necessary database information from there.. The password can be left blank. You can change it. It's not important because it's a local host.
Let's go back to the file "go.php".
The first two lines should be as follows
Require_once in the config.php";
$cookies_sure=86400*4;
The reason that the "config.php" file is separate. If server host is made with Local host, it gives you the convenience of transferring to the server on the Internet. The name of the database password on the server on the Internet will be different. The local host will be different. However, the other files are the same.
Connecting to the database with the following code and the adjustment is in progress.
$con = new PDO("mysql:host=$h_name;dbname=$dbnames;Charset=UTF8", $usr_root, $usr_pass
, Array(
PDO::ATTR_EMULATE_PREPARES=>false,
PDO::MYSQL_ATTR_DIRECT_QUERY=>false,
PDO::ATTR_ERRMODE=>::ERRMODE PDO_EXCEPTION
));
$tabayar="ENGINE=InnoDB Default Charset=UTF8mb4 COLLATE=UTF8mb4_general_ci;";
The UTF-8 adjustment is important and there will be no compatibility issue if the "charset" setting is as above. "InnoDB" is usually appropriate for most servers.
The following code for the continuation of the compound
$white globe_tabad_users="c_vids";
$sql = "CREATE TABLE IF NOT EXISTS $white globe_tabad_users (
'Id' int(11) NOT NULL AUTO_INCREMENT,
'V1' VARCHAR(42) NOTE NULL,
'V2' MEDIUMTEXT NULL DEFAULT NULL,
'V3' MEDIUMTEXT NULL DEFAULT NULL,
'V4' MEDIUMTEXT NULL DEFAULT NULL,
'V5' MEDIUMTEXT NULL DEFAULT NULL,
Index the index of the unique '1' ('id'),
PRIMARY KEY ('v1')
) $tabayar ";
$con->exec($sql);
Above table was created for a database. The table is created as "c_vids".
Features available in:
- The unique INDEX // Unique key for authentication
- PRIMARY KEY // the key
- AUTO_INCREMENT // automatic number increase
Check to see if that works. In the browser url: http://127.0.0.1/go.php?
If there is a fault will be there. If there are no errors, the blank screen will come. After looking at the URL, refresh it in the database this way. Check to see if it's working properly
You can check the details when you click. You can make various comparisons. You can also change it here. But it would be better if you didn't change it here. The database generated in the form of coding will always be better.
You can look as work as above. Then we can make a small database sample. We can finish it later. We can continue the interragation of "data_send_1" right now.
Try the code at the bottom of the "go.php " file as follows.
Update ”data_send_1" section
$query="data_send_1";
If(isset($_GET[$query)){if($_GET[$query]=='1'){
$v1=get_data("v1");
$v2=get_data("v2");
If($v1 && $v2){
$tabad='c_vids';
$sql="INSERT into $tabad(v1,v2) VALUES('$v1','$v2')";
$ins = $con->prepare($sql);
$ins->execute();
}
}}
The above is simple-you see the basic database entry and registration method. Pdo method General-widely used. That's many people use it.
Run "http://127.0.0.1/" ie "index.php " with the browser. Then check the registry with "Laragon-database ". If not, refresh (if empty). If not, open the "go.php " section with the browser and it will seem if there are errors observe.
If you can see data as above, it shows a good result. If it is empty you can start over again or read the messages about the error. In the later lesson, JSON will be shown to process much more complex data types-saving. More technical issues will begin in the next lesson.Now the end.