Categories
Operating Systems

Testing and Elevating Admin Privileges in Windows 7 / Windows 2008

Introduction

Due to UAC , many processes, including many installations, must be run from an elevated privilege level, even when you are logged in as an administrator.

Testing for Privileges

The following code in an installation script will test to see if the script is operating

::Test whether script has Admin Privileges/is elevated
at > nul:
If "%errorlevel%"=="0" (
  echo You are Administrator
) else (
  echo You are NOT Administrator. Exiting...
  Ping 127.0.0.1 > nul: 2>&1
  Exit /B 1
)

Elevating a Session

You can run any executable by right-clicking it and selecting Run as Administrator.  However, you may find that you want to simply elevate your session.  (Think before you do this, there's a reason they have UAC.)

The following process details how to elevate a session;

  • Log in as admin
  • Open an elevated CMD window
  • Right-Click on Command Prompt in the Start menu and click Run as Administrator.
  • Kill the explorer proccess from Task Manager or enter the following command at the elevated command prompt. (/F forcefully terminates the process and /IM represents the 'image name' of the process to be killed, i.e. explorer.exe )
    • taskkill /F /IM explorer.exe
    • In the elevated CMD window, start a new explorer process explorer.exe
    • Close the elevated CMD window
    • Since the explorer process is now running elevated, subsequent operations should not require elevation.

In fewer words:

Log in as admin
From an elevated command window enter the following 3 commands:

taskkill /IM explorer.exe /F
explorer
exit

 

Categories
Web Development

Make your .php extension optional

Place the following code in your .htaccess file to make the .php extension on your pages optional.

RewriteEngine on
# ----- make .php extension optional -----
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ $1.php [QSA,L]
# ---- /make .php extension optional -----

Further suggestions on URL rewriting can be found these articles:

Categories
MySQL

Foreign Keys and Referential Integrity in MySQL

This TechRepublic article is a good fast introduction to the topic of Referential Integrity and the use of  Foreign Keys in MySQL.

Categories
PHP

Test for Database Table in PHP

Sometimes it's handy to be able to detect whether a given table exists, such as when you want to create a table if it isn't present.

The following example will detect the presence of a table in the simplest way I can think of.

 if(mysql_num_rows( mysql_query("SHOW TABLES LIKE 'tablename'"))) {
    // table is present
    // execute some code here
 };

Alternatively, a small change to reverse the logic will let you react if the table does not exist. Note the exclamation point which means "not" and changes the logic to respond if the table does not exist.

 if(!mysql_num_rows( mysql_query("SHOW TABLES LIKE 'tablename'"))) {
    // table is not present
    // here you might want to create the missing table
 };

If you're not familiar with PHP, the logic of the first if() statement says essentially this:

Query the database to see how many tables are named 'tablename'.

Because only one table in the database can have the name, only two responses are possible: 1 or 0. In boolean logic, those values represent Yes/No or True/False respectively and can be acted on.

If the condition is satisfied (yes, the table exists in the first example above or no, the table does not exist in the second example above), the code between the braces {} will be executed.