Archive for the ‘PHP’ Category

PHP nl2p Function

Sunday, August 3rd, 2008

Forget nl2br! With this nl2p function your content will be better formatted and code better presented.

1
2
3
4
5
6
7
function nl2p($string)
{
	$string = "<p>" . $string . "</p>";
	$string = preg_replace("/\r\n\r\n/", "</p>\n\n<p>", $string);
	$string = preg_replace("/\r\n/", "<br />", $string);
	return $string;
}

PHP Login Function

Wednesday, August 15th, 2007

Here is a function in PHP that checks if a user is logged in and if a login attempt has been submitted (and therefore logs them in if verified).

function check_login($username, $password, $required = 1, $redirect = 'https://www.domain.com/login.php')
{
if (isset($_SESSION['user']) AND isset($_SESSION['pass'])) return true;
if (!empty($username) AND !empty($password))
{
$username = stripslashes($username);
$password = stripslashes($password);
$checkq = mysql_query("
SELECT username, password
FROM users
WHERE
username = '" . mysql_real_escape_string($username) . "'
AND password = '" . md5($password) . "'
LIMIT 1
");
if (mysql_num_rows($checkq) &gt; 0)
{
$session_backup = $_SESSION;
unset($_COOKIE[session_name()]);
session_destroy();
session_start();
$_SESSION = $session_backup;
unset($session_backup);
 
$user_row = mysql_fetch_assoc($checkq);
$_SESSION['user'] = $user_row['username'];
$_SESSION['pass'] = $user_row['password'];
unset($user_row);
 
return true;
}
}
if ($required == 1)
{
header("location: $redirect");
exit();
}
return false;
}