Author: Nako

Access MySQL with PHP in XAMPP

GOAL

Apply to access MariaDB(MySQL) with PHP in XAMPP

Environment

Windows10
XAMPP 7.4.10

At first, install XAMPP (“Installation of XAMPP“) and If you’ll use MariaDB in a production environment, make it secure (“How To Set MySQL Password in XAMPP“). And I create database “photo_sharing” according to “How to Create Database of MariaDB in XAMPP“.

About PDO

PDO, PHP Data Objects, is an extension to access database in PHP.

Reference: PHP Data Objects in PHP manual

(more…)

How to Create Database of MariaDB in XAMPP

GOAL

Today’s goal is create database with MariaDB.

Environment

Windows10
XAMPP 7.4.10

If you don’t have XAMPP, please install it first according to “Installation of XAMPP“. And If you’ll use MariaDB in a production environment, make it secure by setting password according to “How To Set MySQL Password in XAMPP“.

Method

1. Open phpMyAdmin

Start XAMPP Control Panel and run Apache and MariaDB.

Access localhost/phpmyadmin/ or click Admin button of MySQL.

(more…)

How To Set MySQL Password in XAMPP

GOAL

To set MySQL password in XAMPP to make it secure.

Environment

Windows10
XAMPP 7.4.10

If you don’t have XAMPP, please install it first according to “Installation of XAMPP“.

XAMPP security

Please refer to “Is XAMPP production ready?” in Windows Frequently Asked Questions for information. Though it contains some old contents, but it says why default XAMPP shouldn’t be used in a production environment.

Method

1. Run Apache and MySQL

Start XAMPP Control Panel and run Apache and MySQL.

2. Access phpMyAdmin and change password

Access localhost/phpmyadmin with your browser. And click “User accounts”.

Click “Edit privileges” for root@localhost.

Click “Change password”.

Input your password. You can generate highly safe password with Generate button. And click “Go” button.

Close phpMyAdmin.

*An error will occur when accessing phpMyAdmin now because phpMyAdmin doesn’t know the password.

3. Change config.inc.php of phpMyAdmin

Open xampp\phpMyAdmin\config.inc.php and change the value of $cfg[‘Servers’][$i][‘password’].

/* Authentication type and info */
$cfg['Servers'][$i]['auth_type'] = 'config';
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = '';  //input the password here

Save the file and close it.

Confirmation

Open the shell.

And input command “mysql -h localhost -u root -p” and the password you defined.

# mysql -h localhost -u root -p
Enter password:  <input password here>

Confirm that MariaDB is connected.

Input “quit” to quit MySQL.

And confirm that you can access localhost/phpmyadmin.

Next?

How to Create Database of MariaDB in XAMPP

How To Run PHP in XAMPP

I installed XAMPP in Installation of XAMPP. Today, I’ll use XAMPP as a test environment for PHP.

GOAL

To setup XAMPP and start PHP development in XAMPP.

How to start XAMPP

Be sure that the started module is stopped before quitting the control panel.

Method 1 Use xampp/xampp-control.exe

Right-click and run as administrator “xampp/xampp-control.exe” or “XAMPP Control Panel” in start menu.

Click “Start” buttons on the control panel.

If you add modules as a service, the check box is checked.
*You can’t see whether the check box is checked if you run the control panel as a non-administrator.

In this article below, a situation in which all modules are not registered as a service is supposed.

Click “Start” to start Apache and access localhost to confirm that apache is running.

Open browser and input “localhost”
The index page of XAMPP

Method 2 Use batch file

Use batch files in xampp\ directory to start each application individually.

xampp\xampp_start.exe
xampp\xampp_stop.exe
xampp\apache_start.bat
xampp\apache_stop.bat
mysql_start.bat, mercury_start.bat and filezilla_start.bat

Run your php program

The directory “xampp\htdocs” is document root that is assigned to localhost. So you can run your php program by putting the php file here.

Open xampp\htdocs\index.php and you can see the program to redirect users to localhost/dashboard by using header() function.

<?php
	if (!empty($_SERVER['HTTPS']) && ('on' == $_SERVER['HTTPS'])) {
		$uri = 'https://';
	} else {
		$uri = 'http://';
	}
	$uri .= $_SERVER['HTTP_HOST'];
	header('Location: '.$uri.'/dashboard/');
	exit;
?>

1. Create “test” directory in xampp\htdocs.

2. Create PHP files

Create 2 PHP files in the “test” directory, index.php to post input data and posted.php to get and display the input data.

xampp\htdocs\test\index.php

Use POST method to send the message.

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>test</title>
</head>
<body>
	<form action="posted.php" method="POST">
		<label>Input message:</label>
		<input type="text" name="message" /> <input type="submit" value="Submit!" />
	</form>
</body>
</html>

xampp\htdocs\test\posted.php

Get the posted data by using $_POST[<name>].

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>posted</title>
</head>
<body>
	<p>Message "<?php echo $_POST["message"] ?> " is posted!</p>
</form>
</body>
</html>

3. Change the redirect destination URL in xampp\htdocs\index.php

xampp\htdocs\index.php

<?php
	if (!empty($_SERVER['HTTPS']) && ('on' == $_SERVER['HTTPS'])) {
		$uri = 'https://';
	} else {
		$uri = 'http://';
	}
	$uri .= $_SERVER['HTTP_HOST'];
	header('Location: '.$uri.'/test/');  // Change here!
	exit;
?>

4. Access localhost

Access localhost and the page is redirected to localhost/test/. Input any message you like and press submit button.

The posted page will be displayed and you can see the message you input.

How to get PID in Windows10

GOAL

Getting PID, Process ID.

Environment

WIndows10

What is PID?

PID, the process ID, is a number used in some operating system kernels to identify processes uniquely.

Methods

Method 1

Open “Task Manager” and open “Details” tab. See the PID of each Process.

Method 2

Use tasklist command and you can get the list of currently running tasks.

>tasklist

Image Name   PID   Session Name  Session# Mem Usage
============ ===== ============= ========= =========
System Idle  0     Services      0         8 K
System       4     Services      0         2,728 K
Registry     120   Services      0         36,960 K
...

Appendix

How to find the task by using its PID

Use tasklist command as below.

tasklist /svc /fi "PID eq <PID number>"

[Tips] How To Turn Features on/off in Windows10

GOAL

To turn Windows features on and off in Windows10.

Environment

WIndows10

Method

1. Open Control Panel

Press Windows key + R and open “Control Panel”.

Or click Windows System > Control Panel

2. Open “Turn Windows features on or off”

Click “Programs”.

Click “Turn Windows features on or off”.

Select the feature you’d like to turn on or off.

“Apache Service detected with wrong path” in XAMPP

I had some trouble with Apache in XAMPP.

Problem

When I start xampp-control.exe, the message “Apache Service detected with wrong path” is displayed in XAMPP Control Panel.

Environment

Windows10
XAMPP 7.4.10

Cause of the message

Apache Service has already registered and it doesn’t match the Apache of xampp.

Open Windows Administrative Tools > Services.

Open the properties window of Apache2.4 and see the “Path to executable”. It should be changed to the path to the xampp Apache.

*If you’d like to know about “Windows Service” more, check the article “What is Windows Service?“.

Solution

1. Change the register data

Press Windows key + R and Open “regedit”.

Open Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services and open Apache2.4

Right-click ImagePath and Modify the Data.

Change the value data and click OK button.

2. Restart XAMPP Control Panel

Restart XAMPP Control Panel. The error disappeared.

What is Windows Service?

GOAL

To understand the Windows service and how to register an application as a service.

What is Windows service?

Microsoft Windows services, formerly known as NT services, enable you to create long-running executable applications that run in their own Windows sessions. 

from Introduction to Windows Service Applications

These services can be automatically started when the computer boots, can be paused and restarted, and do not show any user interface.

from Introduction to Windows Service Applications

These features make services ideal for use on a server or whenever you need long-running functionality that does not interfere with other users who are working on the same computer.

from Introduction to Windows Service Applications

You can see Windows service applications registered in your PC. Right-click “Start” and click “Computer Management.”

Open Services and you can see the list of Windows service application.

Windows service application contains updater, time setter and security software because these need to be executed automatically. And these applications should be executed without user login and user authority.

Status: Running or not

Srartup Type: Manual, Manual (Trigger Start), Automatic, Automatic (Trigger Start), Automatic (Delayed Start) and so on.

Features of Windows Service

  • It can be started automatically
  • It works in background
  • It works without user interface
  • It can work with any user

What is SCM?

SCM, Service Control Manager is a service for controlling services, which starts, stops, and monitors the status of services. SCM is started by the Winlogon process (winlogon.exe) when Windows starts and starts processes which have “Automatic” startup type.

SCM database

SCM has “SCM database” that is a database of registered services. You can see the SCM database with Register Editor. Press Windows key + R and run “regedit”.

Then you can find the registry “Services” in your computer.

How SCM starts and stops services

How to start

1. SCM starts the service process

2. StartServiceCtrlDispatcher that is the function to connect service to SCM is called in the service process

Then the calling thread, the main thread, is connected to the SCM and the control won’t returned until a services runnning in the process have terminated.

3. The main thread call RegisterServiceCtrlHandlerEx function to handle extended service control requests and create the service thread to execute ServiceMain() function.

4. The service thread starts ServiceMain function when accepting the control request. ServiceMain function is the main function of each service program.

5. The status of the started service is changed to “Running”.

How to end

1. SCM send stop request to the main thread.

2. The main thread stop the service thread and the service thread is removed.

3. The status of the stopped service is changed to “Stopped”.

4. StartServiceCtrlDispatcher is returned.

How to install the service application

Use command below to install with installUtil.

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installUtil.exe <Path to the application>

Use command below to uninstall.

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installUtil.exe /u <Path to the application>

You can use sc command too to install the service.

sc create SVC1 binPath= <Path to the application> DisplayName= "" description= "" start= auto

Sc command is used to check the status of services, create, delete, and manage them.

What is the difference between installUtil and sc?

Refer to some web sites such as the following.

installing windows service with SC.exe or InstallUtil.exe – there is difference but which?

installing windows service with SC.exe or InstallUtil.exe – there is difference but which?

Error with xampp-control.ini in quitting XAMPP

Problem

When I quit XAMPP, an error “Control Panel, an Error: Cannot create file “…\xampp\xampp-control.ini” occured.

And the message “Application Error (Not Responding)” displayed.

Environment

Windows10
XAMPP 7.4.10

What is xampp-control.ini?

If you’d like to know what XAMPP is, please refer the article “Installation of XAMPP“. xampp-control.ini is the configuration settings of XAMPP. It contains variables and values as below.

[Common]
Edition=
Editor=notepad.exe
Browser=
Debug=0
Debuglevel=0
TomcatVisible=1
Language=English
[EnableModules]
Apache=1
MySQL=1
FileZilla=1
Mercury=1
Tomcat=1

What is the meaning of error message?

The error message says “the access to xampp-control.ini was denied” and “the access violation occurred in the module xampp-control.exe”.

Cause of the error

Current user is unauthorized to the file “xampp-control.ini”.

Solution

Change the current user to the administrator

Run “xampp-control.exe” as a administrator. This is the best way.

Appendix

Another Solution: Change permission of xampp-control.ini

Right-click “xampp-control.ini” and open property.

Open Security tab and click “Edit” button to change the permission. Add current user and change its permission from “Read” to “Modify”. Refer ‘“Windows cannot access the specified device, path, or file” error when you try to install, update or start a program or file‘ for details.

Warning: Be careful that this may cause security vulnerability. In my case, I run this application in only local environment.

Installation of XAMPP

Let’s develop your site with XAMPP.

GOAL

Today’s goal is to understand what XAMPP is and how to install it. This article doesn’t contain how to use XAMPP or PHP.

Environment

Windows10
XAMPP 7.4.10

What is XAMPP?

XAMPP is a free development environment for PHP. It contains the set of basic free software for web development, such as Apache, MariaDB as a SQL server, PHP, Perl, phpMyAdmin and OpenSSL.

You can see what software is included in XAMPP in the official site.

XAMPP is an acronym for cross platform(X), Apache, MariaDB, PHP and Perl.

How to Install XAMPP

1. Download

Access “Download” page in the official site. And download any version of XAMPP you like.

(more…)