Search
Search titles only
By:
Search titles only
By:
Log in
Register
Search
Search titles only
By:
Search titles only
By:
Menu
Install the app
Install
Forums
New posts
All threads
Latest threads
New posts
Trending threads
Trending
Search forums
What's new
New posts
New ads
New profile posts
Latest activity
Free Ads
Latest reviews
Search ads
Members
Current visitors
New profile posts
Search profile posts
Contact us
Latest ads
Power Lifting Lever Belt
SkullVamp
Updated:
Saturday at 10:32 PM
Ad icon
port.lk Domain for sale
Lankan-Tech
Updated:
Saturday at 3:55 PM
Colombo
Kaduwela - Two Storey House for Sale
dilrasan
Updated:
Thursday at 2:23 PM
Ad icon
Wechat qr verification
Pawan2005
Updated:
Thursday at 1:28 AM
🚀 GOOGLE AI PRO 18 MONTHS ACTIVATION 🚀
sayuru bandara
Updated:
Jun 10, 2026
Electronics
Vehicles
Property
Search
Reply to thread
Forums
General
ElaKiri Help
need a biiiig HELP..........
Get the App
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
<blockquote data-quote="galleline" data-source="post: 4386193" data-attributes="member: 170042"><p>In the simplest case, special characters may simply break your query. In a more extreme case, a hacker might use SQL injections to gain access to your application. So it is important that we escape these special characters with a \ (backslash). That is, insert a backslash before each special character.</p><p> We can escape special characters (prepend backslash) using <span style="color: #003366">mysql_real_escape_string</span> or <span style="color: #003366">addslashes</span> functions. In most cases PHP will this do automatically for you. But PHP will do so only if the <strong><span style="color: red">magic_quotes_gpc</span></strong> setting is set to On in the php.ini file. We first check whether this setting is on or not. If the setting is off, we use <span style="color: #003366">mysql_real_escape_string</span> function to escape special characters. If you are using PHP version less that 4.3.0, you can use the <span style="color: #003366">addslashes</span> function instead. </p><p> <p style="margin-left: 20px"> A MySQL connection is required before using mysql_real_escape_string() otherwise an error of level E_WARNING is generated.</p> <p style="margin-left: 20px"> </p><p> If the magic quotes setting is on, we do not need escape special characters since PHP has already done it for us. We can check the magic_quotes_gpc by using <span style="color: #003366">get_magic_quotes_gpc</span> function.</p><p> <?php</p><p>if(!<a href="http://www.php.net/get_magic_quotes_gpc" target="_blank">get_magic_quotes_gpc</a>()) {</p><p> $login=<a href="http://www.php.net/mysql_real_escape_string" target="_blank">mysql_real_escape_string</a>($_POST['login']);</p><p>}else {</p><p> $login=$_POST['login'];</p><p>}</p><p>?> <strong>A simple function to escape special characters</strong></p><p></p><p> You can use the function below to clean and prepare data for queries. The function goes through the following steps:</p><p> <ol> <li data-xf-list-type="ol">Trims the string to remove leading and trailing spaces</li> <li data-xf-list-type="ol">If you set the second parameter as true, it will also encode all characters which have HTML character entity equivalents.</li> <li data-xf-list-type="ol">The function then checks for PHP version. If version is greater than or equal to 4.3.0, its uses the <span style="color: #003366">mysql_real_escape_string()</span> function. Otherwise it uses <span style="color: #003366">addslashes()</span> function.</li> </ol><p> <ol> <li data-xf-list-type="ol">Since the mysql_real_escape_string() only works if there is a connection to the MySQL server, we first check whether we are connected to MySQL server by using the <span style="color: #003366">mysql_ping()</span> function.</li> </ol><p> <?php</p><p>function clean($str, $encode_ent = false) {</p><p> $str = @<a href="http://www.php.net/trim" target="_blank">trim</a>($str);</p><p> if($encode_ent) {</p><p> $str = <a href="http://www.php.net/htmlentities" target="_blank">htmlentities</a>($str);</p><p> }</p><p> if(<a href="http://www.php.net/version_compare" target="_blank">version_compare</a>(<a href="http://www.php.net/phpversion" target="_blank">phpversion</a>(),'4.3.0') >= 0) {</p><p> if(<a href="http://www.php.net/get_magic_quotes_gpc" target="_blank">get_magic_quotes_gpc</a>()) {</p><p> $str = <a href="http://www.php.net/stripslashes" target="_blank">stripslashes</a>($str);</p><p> }</p><p> if(@<a href="http://www.php.net/mysql_ping" target="_blank">mysql_ping</a>()) {</p><p> $str = <a href="http://www.php.net/mysql_real_escape_string" target="_blank">mysql_real_escape_string</a>($str);</p><p> }</p><p> else {</p><p> $str = <a href="http://www.php.net/addslashes" target="_blank">addslashes</a>($str);</p><p> }</p><p> }</p><p> else {</p><p> if(!<a href="http://www.php.net/get_magic_quotes_gpc" target="_blank">get_magic_quotes_gpc</a>()) {</p><p> $str = <a href="http://www.php.net/addslashes" target="_blank">addslashes</a>($str);</p><p> }</p><p> }</p><p> return $str;</p><p>}</p><p>?> <strong>Query the database</strong></p><p></p><p> Next we formulate the query which will test whether a user with this login and password exists.</p><p> Note that we are not storing passwords in the database as plain text. Instead we are storing the md5 hash of the password. Use <span style="color: #003366">md5</span> function to create a 32 character hash of any string. md5 is one way encryption. That is, once the password is encrypted, there is no way to decrypt it.</p><p> So if a md5 hash can not be decrypted, how do we compare the user submitted password with the one in the database? The answer is that we simply <u>generate a md5 hash of the user submitted password and then compare this hash to the one stored in the database</u>.</p><p> <?php</p><p>$qry="SELECT member_id FROM members WHERE login='$login' </p><p>AND passwd='".<a href="http://www.php.net/md5" target="_blank">md5</a>($_POST['password'])."'";</p><p>$result=<a href="http://www.php.net/mysql_query" target="_blank">mysql_query</a>($qry);</p><p>?> The query will return a result set with a single row if the login details are correct and zero rows if the login details are incorrect. Use <span style="color: #003366">mysql_num_rows</span> to find out the number of rows in the result set and hence determine whether the login details were correct or not.</p><p> <strong>Store authentication status in session</strong></p><p></p><p> Once we know that the login details are correct, we need to store this information somewhere, so that the subsequent pages know that the user has been authenticated successfully. We use PHP session for this purpose.</p><p> Retrieve the member’s ID from the result set and store it in the session as SESS_MEMBER_ID. Subsequent pages will just need to test for the existence of SESS_MEMBER_ID in the session to verify the authentication status of the user. After storing the member ID in the session, redirect the user to the member-index.php page.</p><p> <?php</p><p>if(<a href="http://www.php.net/mysql_num_rows" target="_blank">mysql_num_rows</a>($result)>0) {</p><p> //Login Successful</p><p> //Regenerate session ID to</p><p> //prevent session fixation attacks</p><p> <a href="http://www.php.net/session_regenerate_id" target="_blank">session_regenerate_id</a>();</p><p> $member=<a href="http://www.php.net/mysql_fetch_assoc" target="_blank">mysql_fetch_assoc</a>($result);</p><p> $_SESSION['SESS_MEMBER_ID']=$member['member_id'];</p><p> //Write session to disc</p><p> <a href="http://www.php.net/session_write_close" target="_blank">session_write_close</a>();</p><p> <a href="http://www.php.net/header" target="_blank">header</a>("location: member-index.php");</p><p> <a href="http://www.php.net/exit" target="_blank">exit</a>();</p><p>}</p><p>?> If the login fails, redirect the user to login-failed.php page.</p><p> <strong>Preventing session fixation attacks</strong></p><p></p><p> Once we have ascertained that the user supplied login details are correct, we store his ID in a session variable named SESS_MEMBER_ID. But we before we do that, we call the <strong>session_regenerate_id()</strong> function. This function generates a new session ID while keeping intact any information stored in the session.</p><p> <strong>How to authenticate individual pages</strong></p><p></p><p> As mentioned above, the presence or absence of SESS_MEMBER_ID in the session will tell us whether the user is logged in or not. If a variable names SESS_MEMBER_ID exists in the session, then the user has been logged in and authenticated. I have moved this logic to a separate PHP script, auth.php</p><p> <?php</p><p>//Start session</p><p><a href="http://www.php.net/session_start" target="_blank">session_start</a>();</p><p>//Check whether the session variable</p><p>//SESS_MEMBER_ID is present or not</p><p>if(!<a href="http://www.php.net/isset" target="_blank">isset</a>($_SESSION['SESS_MEMBER_ID']) || </p><p> (<a href="http://www.php.net/trim" target="_blank">trim</a>($_SESSION['SESS_MEMBER_ID'])=='')) {</p><p> <a href="http://www.php.net/header" target="_blank">header</a>("location: access-denied.php");</p><p> <a href="http://www.php.net/exit" target="_blank">exit</a>();</p><p>}</p><p>?> Now we can just include the auth.php file in any page we want to password protect. See member-index.php and member-profile.php page for examples.</p><p> <strong>How to logout the user</strong></p><p></p><p> To logout the user, simply unset the SESS_MEMBER_ID variable. See the logout.php script for example.</p><p> <?php</p><p>//Start session</p><p><a href="http://www.php.net/session_start" target="_blank">session_start</a>();</p><p>//Unset the variable SESS_MEMBER_ID stored in session</p><p><a href="http://www.php.net/unset" target="_blank">unset</a>($_SESSION['SESS_MEMBER_ID']);</p><p>?> <strong>PHP login script</strong></p><p></p><p></p><p>mama hitanne ne kara ganna puluwan wei kiyala <img src="/styles/default/xenforo/smilies/default/sad.gif" class="smilie" loading="lazy" alt=":(" title="Sad :(" data-shortname=":(" /></p></blockquote><p></p>
[QUOTE="galleline, post: 4386193, member: 170042"] In the simplest case, special characters may simply break your query. In a more extreme case, a hacker might use SQL injections to gain access to your application. So it is important that we escape these special characters with a \ (backslash). That is, insert a backslash before each special character. We can escape special characters (prepend backslash) using [COLOR=#003366]mysql_real_escape_string[/COLOR] or [COLOR=#003366]addslashes[/COLOR] functions. In most cases PHP will this do automatically for you. But PHP will do so only if the [B][COLOR=red]magic_quotes_gpc[/COLOR][/B] setting is set to On in the php.ini file. We first check whether this setting is on or not. If the setting is off, we use [COLOR=#003366]mysql_real_escape_string[/COLOR] function to escape special characters. If you are using PHP version less that 4.3.0, you can use the [COLOR=#003366]addslashes[/COLOR] function instead. [INDENT] A MySQL connection is required before using mysql_real_escape_string() otherwise an error of level E_WARNING is generated. [/INDENT] If the magic quotes setting is on, we do not need escape special characters since PHP has already done it for us. We can check the magic_quotes_gpc by using [COLOR=#003366]get_magic_quotes_gpc[/COLOR] function. <?php if(![URL="http://www.php.net/get_magic_quotes_gpc"]get_magic_quotes_gpc[/URL]()) { $login=[URL="http://www.php.net/mysql_real_escape_string"]mysql_real_escape_string[/URL]($_POST['login']); }else { $login=$_POST['login']; } ?> [B]A simple function to escape special characters[/B] You can use the function below to clean and prepare data for queries. The function goes through the following steps: [LIST=1] [*]Trims the string to remove leading and trailing spaces [*]If you set the second parameter as true, it will also encode all characters which have HTML character entity equivalents. [*]The function then checks for PHP version. If version is greater than or equal to 4.3.0, its uses the [COLOR=#003366]mysql_real_escape_string()[/COLOR] function. Otherwise it uses [COLOR=#003366]addslashes()[/COLOR] function.[/LIST] [LIST=1] [*]Since the mysql_real_escape_string() only works if there is a connection to the MySQL server, we first check whether we are connected to MySQL server by using the [COLOR=#003366]mysql_ping()[/COLOR] function.[/LIST] <?php function clean($str, $encode_ent = false) { $str = @[URL="http://www.php.net/trim"]trim[/URL]($str); if($encode_ent) { $str = [URL="http://www.php.net/htmlentities"]htmlentities[/URL]($str); } if([URL="http://www.php.net/version_compare"]version_compare[/URL]([URL="http://www.php.net/phpversion"]phpversion[/URL](),'4.3.0') >= 0) { if([URL="http://www.php.net/get_magic_quotes_gpc"]get_magic_quotes_gpc[/URL]()) { $str = [URL="http://www.php.net/stripslashes"]stripslashes[/URL]($str); } if(@[URL="http://www.php.net/mysql_ping"]mysql_ping[/URL]()) { $str = [URL="http://www.php.net/mysql_real_escape_string"]mysql_real_escape_string[/URL]($str); } else { $str = [URL="http://www.php.net/addslashes"]addslashes[/URL]($str); } } else { if(![URL="http://www.php.net/get_magic_quotes_gpc"]get_magic_quotes_gpc[/URL]()) { $str = [URL="http://www.php.net/addslashes"]addslashes[/URL]($str); } } return $str; } ?> [B]Query the database[/B] Next we formulate the query which will test whether a user with this login and password exists. Note that we are not storing passwords in the database as plain text. Instead we are storing the md5 hash of the password. Use [COLOR=#003366]md5[/COLOR] function to create a 32 character hash of any string. md5 is one way encryption. That is, once the password is encrypted, there is no way to decrypt it. So if a md5 hash can not be decrypted, how do we compare the user submitted password with the one in the database? The answer is that we simply [U]generate a md5 hash of the user submitted password and then compare this hash to the one stored in the database[/U]. <?php $qry="SELECT member_id FROM members WHERE login='$login' AND passwd='".[URL="http://www.php.net/md5"]md5[/URL]($_POST['password'])."'"; $result=[URL="http://www.php.net/mysql_query"]mysql_query[/URL]($qry); ?> The query will return a result set with a single row if the login details are correct and zero rows if the login details are incorrect. Use [COLOR=#003366]mysql_num_rows[/COLOR] to find out the number of rows in the result set and hence determine whether the login details were correct or not. [B]Store authentication status in session[/B] Once we know that the login details are correct, we need to store this information somewhere, so that the subsequent pages know that the user has been authenticated successfully. We use PHP session for this purpose. Retrieve the member’s ID from the result set and store it in the session as SESS_MEMBER_ID. Subsequent pages will just need to test for the existence of SESS_MEMBER_ID in the session to verify the authentication status of the user. After storing the member ID in the session, redirect the user to the member-index.php page. <?php if([URL="http://www.php.net/mysql_num_rows"]mysql_num_rows[/URL]($result)>0) { //Login Successful //Regenerate session ID to //prevent session fixation attacks [URL="http://www.php.net/session_regenerate_id"]session_regenerate_id[/URL](); $member=[URL="http://www.php.net/mysql_fetch_assoc"]mysql_fetch_assoc[/URL]($result); $_SESSION['SESS_MEMBER_ID']=$member['member_id']; //Write session to disc [URL="http://www.php.net/session_write_close"]session_write_close[/URL](); [URL="http://www.php.net/header"]header[/URL]("location: member-index.php"); [URL="http://www.php.net/exit"]exit[/URL](); } ?> If the login fails, redirect the user to login-failed.php page. [B]Preventing session fixation attacks[/B] Once we have ascertained that the user supplied login details are correct, we store his ID in a session variable named SESS_MEMBER_ID. But we before we do that, we call the [B]session_regenerate_id()[/B] function. This function generates a new session ID while keeping intact any information stored in the session. [B]How to authenticate individual pages[/B] As mentioned above, the presence or absence of SESS_MEMBER_ID in the session will tell us whether the user is logged in or not. If a variable names SESS_MEMBER_ID exists in the session, then the user has been logged in and authenticated. I have moved this logic to a separate PHP script, auth.php <?php //Start session [URL="http://www.php.net/session_start"]session_start[/URL](); //Check whether the session variable //SESS_MEMBER_ID is present or not if(![URL="http://www.php.net/isset"]isset[/URL]($_SESSION['SESS_MEMBER_ID']) || ([URL="http://www.php.net/trim"]trim[/URL]($_SESSION['SESS_MEMBER_ID'])=='')) { [URL="http://www.php.net/header"]header[/URL]("location: access-denied.php"); [URL="http://www.php.net/exit"]exit[/URL](); } ?> Now we can just include the auth.php file in any page we want to password protect. See member-index.php and member-profile.php page for examples. [B]How to logout the user[/B] To logout the user, simply unset the SESS_MEMBER_ID variable. See the logout.php script for example. <?php //Start session [URL="http://www.php.net/session_start"]session_start[/URL](); //Unset the variable SESS_MEMBER_ID stored in session [URL="http://www.php.net/unset"]unset[/URL]($_SESSION['SESS_MEMBER_ID']); ?> [B]PHP login script[/B] mama hitanne ne kara ganna puluwan wei kiyala :( [/QUOTE]
Insert quotes…
Verification
Payakata winadi keeyak tibeda?
Post reply
Top
Bottom