Using cookies in PHP

How to enable sessions in PHP with cookies

Cookies are all around in our everyday life while we browse the Internet. Most people wouldn’t know much about them, if not for those “our website uses cookies to be operational” signs that are on mostly any page now since GDPR. Cookies have a long history if sometimes being good, sometimes bad. As it is usual with most aspects of the world, this technology can be used in a good or bad way.

In this tutorial we will explore cookies a bit from the server side programming perspective: we’ll create an example cookie in PHP, and store the visitor’s chosen username in it. The cookie will reside in the browser of the visitor, so on the next visit we can read it and use it to greet the visitor on the username submitted.

Our simple webpage will recognize the visitor as long as the cookie is present in the browser. We do not store any information on the server side: if the visitor first checks in from a PC, and next time from a smartphone, we will not know it is the same individual. This is one of the main point with cookies: we can only identify the given client, and only as long as the data is provided by that particular browser.

In this tutorial you will learn:

  • How to setup the test environment
  • How to write a simple example program that uses cookies
  • How to test the workings of the program
  • How to reset the environment

Read more

Bash Script Comment Example

How to write comments in Bash Scripts

Well you have written an awesome Bash script. It works perfectly and perhaps there is no need to add new functionality. Well, maybe not for now, at least! At this stage you are happy with the script. However, after few months you reopen your bash script again to add new feature and you get a headache to figure out on how the script actually works. Hence, you need to spend additional energy and time before you can actually start editing the script in order to add new feature.

Well, at this point you regret that you did not put some comments ( notes ) into the script to remind you of how the bash script is structured. Not only that Bash comments serve as excellent notes for you or anybody else who might work with your script they may be to some extend also used as a basic bash script debugging tool.

In this tutorial you will learn:

  • How to comment on bash command line
  • How to write comment bash scripts
  • How to create multiple line comments

Read more

Bash Terminal

How to Debug Bash Scripts

There are techniques from traditional programming environments that can help.
Some basic tools like using an editor with syntax highlighting will help as well.
There are builtin options that Bash provides to make debugging and your everyday Linux System Administration job easier.

In this article you will learn some useful methods of debugging Bash scripts:

  • How to use traditonal techniques
  • How to use the xtrace option
  • How to use other Bash options
  • How to use trap

Read more

Recording data trough browser

Persisting data into a PosgreSQL database with PHP

PHP is a widely used server scripting language. It’s vast capabilities and lightweight nature makes it ideal for web frontend developing, and while it is possible to create advanced structures in it, it’s basic usage also easy to learn, which makes it a good starting point for beginners also. The data recorded, processed, sent or displayed by the application we build is need to be stored somewhere, however; our webpage is stateless without this data. We can store our data in many ways or even discard it after use, but the most standard way is storing it in a database designed for the sole purpose of storing data in a reliable way, and present it if needed, as fast as possible.

In this tutorial we will build a simple webpage to record and display user data. We will use a PostgreSQL RDBMS as backend, and develop our application in PHP that will run on an Apache Webserver. This way we can access our web application from any common browser to view or add to our user database. It is the nature of the web application that many users/operators can work with it simultaneously, all they need is a browser and network access to our application.

In this tutorial you will learn:

  • How to install needed packages for PostgreSQL connection in PHP
  • How to create the database side of the application
  • How to code the example application
  • How to operate and verify the example application

Read more

C development on Linux – Comparison between C and other programming languages – II.

Introduction

Since in our first part of this article we said we expected you, the reader, to have some programming knowledge, in this part we want to help you get a on where C stands in comparison to other programming languages you might know. The choice of those languages was pretty tough because of various criteria, but in the end we stopped at C++, Perl and Python. Since programming languages can be classified in lots of ways (depending on paradigm, syntax or style, for example), we didn’t try to find languages that are in the same category as C. Instead, since the aforementioned languages are pretty popular in the Linux world, we chose them, mentioning what every language’s place is in the great scheme, what it’s generally used for and of course, the differences between them and C. This article will be structured as follows: we’ll start with important elements of every language, like variable declaration, typing or structure and compare that with how it’s done with C. Thusly we hope to give you an idea about the language before we start. The parts this article is made of will be exactly like the announced structure of this article, to make comprehension easier.

The comparison

Types, operators, variables

C++

C++ was initially named “C with classes”, which says a lot about its’ relation with C. It is widely seen as a superset of C ( thus C++ the unary increment operator ++ ) , introducing object-oriented programming features. Types are essentially used the same, with bool being introduced in C++ for boolean operations. Basically, when talking about C and C++ differences, most of those come from the OOP concepts C++ has and C does not. For example C++ has operator overloading, an OOP-specific term, which means that an operator may have different implementations depending on the data it’s operating on. For example, in C++ you can do this:

 
a << 2

Now, if a is an integer, this will do a bitwise operation on it (left shift by 2), but if a is an output stream, the above line will try to write a ‘2’ to it. This kind of behavior is one of the reason C++ is criticized for allowing poor programming practices. Variables and constants are declared the same way as in C.

Read more

javascript-logo

Javascript promises tutorial with examples

While previously available via third party libraries, promises were introduced in Javascript, as a native
feature, with ECMAScript6.

They provide an alternative to callbacks when dealing with asynchronous code, providing,
among the other things, a cleaner way to handle errors. In this tutorial we will see how promises work, how to
create them and how to use their methods.

In this tutorial you will learn:

  • What is a Javascript promise.
  • How to create a Javascript promise.
  • How promises can be used to manage asynchronous code.
  • What are the methods that can be used with a promise.

Read more

rpm

How to perform python input/output operations on files with python open function

The manipulation of files is an operation which sooner or later we’ll need to perform in our programs. The python built-in open function returns a file object, which lets us interact with files in different modes: we will see them in this article.

In this python tutorial you will learn:

  • How to use the python open function.
  • What are the various modes of operation which can be used with the python open function.
  • How to interact with a file object.
  • Why is important to close a file object, and how to do it.

Read more

Python List Methods

Introduction

Using methods with lists gives you the power to manipulate the data stored in the lists quickly and effectively. Many of these methods are concerned with finding, adding, and removing pieces of data stored within the list they are being called on. Others are more concerned with the structure of the list itself.

In any case, they will make your experience using lists a lot less frustrating by saving you time and effort writing and rewriting the same code.

Finding the Length

Yes, this does start off with some more overlap with strings. The len() method works on lists as well. So, in order to find the amount of elements in a list, just place it in the len() method.

linux_distros = ['Debian', 'Ubuntu', 'Fedora', 'CentOS', 'OpenSUSE', 'Arch', 'Gentoo']
print(len(linux_distros))

Again, keep in mind that the result is the number of elements in the list. The last element is available at the index of six. You can also use the len() method to access elements in the list, if you have to.

linux_distros = ['Debian', 'Ubuntu', 'Fedora', 'CentOS', 'OpenSUSE', 'Arch', 'Gentoo']
print(linux_distros[len(linux_distros) - 1])

Adding the - 1 at the end is necessary, because there is no index of seven, since the list starts counting at zero. Using this method is another way to access elements based on the length of the list. In some cases, it may be preferable to using the negative numbers.

Read more

Python OS Module

Introduction

Python is a powerful scripting language. So, why not use it to script Linux? The os module is Python’s answer to handling many Linux file operations.

The os module allows Python to perform many of the file and folder operations that you’d typically carry out in the Linux command line. It enable you to begin swapping out Bash for Python, which makes for a much cleaner and friendlier scripting experience.

Loading OS

The os module is a Python module like any other. In any script where you want to use it, you can use an import statement to pull it in.

import os

getcwd()

The getcwd() method returns the current working directory in the form of a string. You don’t need to pass it anything. It’s roughly the equivalent of pwd.

print(os.getcwd())

Read more

Python Boolean Operators

Introduction

If you’ve been following along, you’re probably tired of hearing about lists right about now. Well, this guide has nothing to do with lists! Remember back when you first leaned about variables; how there was one that just held True or False called a Boolean? There hasn’t been a guide on them because Booleans are actually that simple. However, there are symbols called Boolean Operators that are used to evaluate whether a statement is true or false. They compare statements and return either true or false. It sounds simple, and in a way it is, but it can get more complex when more comparisons get added.

And

The first comparison operator is and. You can use and to test in one statement and another statement are both true.

is_it_true = (3 * 4 > 10) and (5 + 5 >= 10)
print(is_it_true)

The code prints out True because both 3 * 4 > 10 and 5 + 5 >= 10 are true.

Read more