Wednesday, May 24, 2017

Minus (hour) from a datetime in Mysql



DATE_SUB(in_time, INTERVAL 8 HOUR);

Easy :)

SOURCE

Thanks for watching my blog. Do follow me on google+ or like my facebook page for more! :)
Obstrum Facebook Page

Sunday, May 21, 2017

Create a middleware in laravel




Hi guys, this tutorial will cover on how to create middleware in laravel.

If you guys forget the syntax on how to create a project using laravel, you guys can go to this link.

Create project using Laravel

Alright so let start.

The syntax for creating middleware is

php artisan make:middleware Middlewarename

You should see the middleware at App/Http/Controllers/Middleware

Let said that we want to create a route that only accessible to admin.

1) Create a middleware (php artisan make:middleware Middlewarename)

2) Go to web.php and register a new route.

Route::get('admin', function(){
     echo "You have access";
});

3) Go to middleware that just being created and put session inside

4) Register your middleware in kernel.php

'admin' => \App\Http\Middleware\MiddlewareClassName::class

Note that the namespace path is depends where you store your middleware. Change it according to where you save your middleware

5) Edit web.php to use the middleware that we just created.

Route::get('admin', function(){
     echo "You have access";
})->middleware('admin');

6) You should be good to go

By the way, you can also group your route using one middlware.

Route::group (['middleware' => ['admin']], function() {

      Route::get('admin', function(){
          echo "You have access";
     });

});


Thanks for watching my blog. Do follow me on google+ or like my facebook page for more! :)
Obstrum Facebook Page

Mysql LIMIT Query Optimization technique




1) Make sure it uses index It is very important to have ORDER BY with LIMIT executed without scanning and sorting full result set, so it is important for it to use index

For example if I do SELECT * FROM sites ORDER BY date_created DESC LIMIT 10; I would use index on (date_created) to get result set very fast.  

 2)  Now what if I have something like SELECT * FROM sites WHERE category_id=5 ORDER BY date_created DESC LIMIT 10;
 
In this case index by date_created may also work but it might not be the most efficient – If it is rare category large portion of table may be scanned to find 10 rows. So index on (category_id, date_created) will be better idea.

Thanks for watching my blog. Do follow me on google+ or like my facebook page for more! :)
Obstrum Facebook Page

Can you block direct access to .js file



Answer is you can still block it, but not 100%.

1) Obfuscate the code YUI Compressor
2) If you have direct access to.htaccess, you can rewrite by preventing access to .js file. http://stackoverflow.com/questions/6335644/how-can-i-block-direct-access-to-my-javascript-files
3) Write another .js file and include it to the .js file that you want user to block. So if user access the .js file, the code will not shown.


However, the main case in which I want to really 'hide stuff' is when I'm publishing an email address.
Note, there is the problem of Chrome when you click on 'inspect element'. It will show your original code: every time. This is why obfuscation is generally regarded as being a better way to go.

Thanks for watching my blog. Do follow me on google+ or like my facebook page for more! :)
Obstrum Facebook Page

Websocket vs Socket Simple Expanation




Websocket and Socket are not the same. Even though they achieve (in general) similar things, they are really different.

WebSockets typically run from browsers connecting to Application Server over a protocol similar to HTTP that runs over TCP/IP. So they are primarily for Web Applications that require a permanent connection to its server. On the other hand, plain sockets are more powerful and generic. They run over TCP/IP but they are not restricted to browsers or HTTP protocol. They could be used to implement any kind of communication.


Thanks for watching my blog. Do follow me on google+ or like my facebook page for more! :)
Obstrum Facebook Page

Useful InnoDB optimizing tips!

 

 

9.5.1 Optimizing Storage Layout for InnoDB Tables

  • Once your data reaches a stable size, or a growing table has increased by tens or some hundreds of megabytes, consider using the OPTIMIZE TABLE statement to reorganize the table and compact any wasted space. The reorganized tables require less disk I/O to perform full table scans. This is a straightforward technique that can improve performance when other techniques such as improving index usage or tuning application code are not practical.
    OPTIMIZE TABLE copies the data part of the table and rebuilds the indexes. The benefits come from improved packing of data within indexes, and reduced fragmentation within the tablespaces and on disk. The benefits vary depending on the data in each table. You may find that there are significant gains for some and not for others, or that the gains decrease over time until you next optimize the table. This operation can be slow if the table is large or if the indexes being rebuilt do not fit into the buffer pool. The first run after adding a lot of data to a table is often much slower than later runs.
  • In InnoDB, having a long PRIMARY KEY (either a single column with a lengthy value, or several columns that form a long composite value) wastes a lot of disk space. The primary key value for a row is duplicated in all the secondary index records that point to the same row. (See Section 15.8.9, “Clustered and Secondary Indexes”.) Create an AUTO_INCREMENT column as the primary key if your primary key is long, or index a prefix of a long VARCHAR column instead of the entire column.
  • Use the VARCHAR data type instead of CHAR to store variable-length strings or for columns with many NULL values. A CHAR(N) column always takes N characters to store data, even if the string is shorter or its value is NULL. Smaller tables fit better in the buffer pool and reduce disk I/O.
    When using COMPACT row format (the default InnoDB format) and variable-length character sets, such as utf8 or sjis, CHAR(N) columns occupy a variable amount of space, but still at least N bytes.
  • For tables that are big, or contain lots of repetitive text or numeric data, consider using COMPRESSED row format. Less disk I/O is required to bring data into the buffer pool, or to perform full table scans. Before making a permanent decision, measure the amount of compression you can achieve by using COMPRESSED versus COMPACT row format. 

Thanks for watching my blog. Do follow me on google+ or like my facebook page for more! :)
Obstrum Facebook Page

"Openssl extension is required error " FIXED!



 [Composer\Exception\NoSslException]
The openssl extension is required for SSL/TLS protection but is not available. If you can not enable the openssl extension, you can disable this error, at your own risk, by setting the 'disable-tls' option to true.


navigate to php.ini (Note that php 7 have php.ini-development and php.ini-production, choose one of this and rename it as php.ini)

Set your path (Line 738)

extension_dir = "C:\EntryPass\Web\php\ext\" ( Make sure that your path is correct, if not it will nto work)

You should be able to create laravel project now. The openssl problem should gone.

Thanks for watching my blog. Do follow me on google+ or like my facebook page for more! :)
Obstrum Facebook Page