18 August 2013

PHP Get Server OS Name

The operating system name where the php running on can be read by several ways;
echo PHP_OS;

// output: CentOS

But if we want to get more detailed information about the OS, we may use php_uname
 
echo php_uname('s');

// output: Linux 2.6.18-348.6.1.el5 x86_64

php_uname gives information including; OS name, OS version, if it is 32bit or 64bit.

17 August 2013

PHP Check If Form Submitted

There are several ways to check whether a form submitted or not.

if(isset($_POST))
{
// do work
}

if (!empty($_POST))
{
// do work
}
 

But it is much better to check the server parameter. There are pretty much information stored in $_SERVER.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
{
// now we are pretty sure for the post action 
}
 
 


Request method is just more rock solid way to check is a form submitted or not.

15 August 2013

PHP Get Referrer Url

in Php $_SERVER array consists of various data in it. Such as;
  • SERVER_NAME
  • REQUEST_METHOD
  • QUERY_STRING
  • HTTP_REFERER
Where the  HTTP_REFERER is the page address which the user agent referred.
Note that it is not guaranteed that this referrer set.
If the visitor comes from the bookmark, it will not be set.

So if it is set, we can get the url of the referrer by ;

$url="";
if(isset($_SERVER["HTTP_REFERER"]))
    $url = $_SERVER["HTTP_REFERER"];


Why we need this referrer url ?
Well I use this referrer url to redirect user to a certain page after a certain job done.
For example, log in user, than redirect to user to his/her profile page.

Example;

$url="";
if(isset($_SERVER["HTTP_REFERER"]))
    $url = $_SERVER["HTTP_REFERER"];

// ... do some job here ...

Header("Location: ".$url);
exit();

This may helpful when a user tries to access a log in required content, after log in successful he/she will be redirected to the previous page and continue whatever he/she will do on that page.

Note that, it is not always a good idea to trust this referrer url, referrer url should be stored in SESSION or cookie will be more secure.

PHP Get Page Parameter Safe $_GET

Most of us have to use the $_GET in PHP to get the desired content id and print the contents on page. But we cannot trust the incoming paremeter directly from the url.
We should check the parameter and validate it.

If it is set
Check if the parameter is set or not
if ( isset($_GET['id']) )
{
 // ok it is set.
}

Now I also check if it is empty or not;

If it is not empty

if ( isset($_GET['id']) && !empty($_GET['id']) )
{
 // ok it is set and not empty
}

If you know the parameter must be integer, than lets check
If it is a valid number. 

if ( isset($_GET['id']) && !empty($_GET['id']) && is_numeric($_GET['id']) )
{
 // ok it is set, not empty, and a number, get it;
$id = trim($_GET['id']);
}

Finally we get our parameter safely in PHP.

PHP Get Current URL

I need to get the current url for some purposes, such as;
  • Check the url if it is used to be. This is required to prevent dublicated content from different urls in your website.
  • Generate social bookmark and like data-href links
It is easy to get the current url in PHP;

<?php 

echo "http://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];

?>


This will give you the exact url of the current page.

29 June 2011

Maximum Request Length Exceeded ASP.NET

I got this error Maximum request length exceeded while i was trying to upload several files, or a single big size file. As default, max file upload size is 4MB.
We can easily have a solution by not touching our asp.net c# source code.

Just add a single line of code in your Web.config file, and you are done.

<configuration>
  <system.web>
    <httpRuntime maxRequestLength="32768" />
  </system.web>
</configuration>


Now we will not get maximum request length exceeded error in asp.net.

14 April 2011

C# Convert String to Integer

Converting string to int is simple in c#. There are two comman ways to convert string to int.
  • Convert.ToInt32
  • Int32.TryParse
Here is the screen shot of our string to int converter sample application:


Note that our integer number must be between −2,147,483,648 and +2,147,483,647 range.

The c# source code to convert string to integer:
private void btnConvert_Click(object sender, EventArgs e)
{
int numValue = 0;
string strValue = string.Empty;

strValue = tbInput.Text.Trim();

try
{
numValue = Convert.ToInt32(strValue);
lblResult.Text = numValue.ToString();
}
catch (Exception exc)
{
tbInput.Text = string.Empty;
lblResult.Text = string.Empty;
MessageBox.Show("Error occured:" + exc.Message);
}
}       

private void btnTryParse_Click(object sender, EventArgs e)
{
ConvertToIntTryParse();
}

private void ConvertToIntTryParse()
{
int numValue = 0;
bool result = Int32.TryParse(tbInput.Text, out numValue);
if (true == result)
lblResult.Text = numValue.ToString();
else
MessageBox.Show("Cannot parse string as int number");
}

I think IntTryParse is better to convert string to int, because it handles the parse error inside and returns bool value if the string is parsable to int.
IntTryParse return boolean value, so that if it parses the string it returns true, else returns false.

Note the parsing string with Convert.ToInt32 may cause a FormatException if the parse string value is not suitable.