September 29, 2013

Log in script load users name

User2827404’s Question:

ive just built a php and mysql log in script which forwards to a members area. I now want the members name that loged in to be displayed, somthing like welcome Stephen for example.

what would be the best way to do this?

ok this is my code once the submit button has been pressed:

`<?php

$host="localhost"; // Host name
$username="stephen2_phptest"; // Mysql username
$password="********"; // Mysql password
$db_name="stephen2_phptest"; // Database name
$tbl_name="registers"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];


// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM registers WHERE email='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:members.php");
}
else {
echo "Wrong Username or Password";
}
?>`

And this is the code for the members area:

`<?php

$host="localhost"; // Host name
$username="stephen2_phptest"; // Mysql username
$password="********"; // Mysql password
$db_name="stephen2_phptest"; // Database name
$tbl_name="registers"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
session_start();
if(!session_is_registered(myusername)){
header("location:index.php");

$myusername=$_POST['myusername'];
}
?>
<html>
<link rel="stylesheet" type="text/css" href="../php/css/styles.css">
<body>
    <div class="members-screen">
Login Successful</br>
Welcome [persons name to load here]<?php echo $_POST['myusername'] ?> <a href="../php/logout.php"> | Logout</a>
<div class="menu">
    <div class="menu-btn">
        <a href="">Home</a>
        </div>
        <div class="menu-btn">
            <a href="">Search</a>
            </div>
            <div class="menu-btn">
                <a href="">Messages</a>
                </div>
                <div class="menu-btn">
                    <a href="">Matches</a>
                    </div>
                    <div class="menu-btn">
                        <a href="">My Account</a>
                        </div>
    </div>
</div>
</body>
</html>`

Store the name on the session when the user logs in and use it when it is needed to be showed.

session_start();

// Your login process

if($valid == true) {
   $_SESSION['logged_user'] = 'stephen'; // Fetch name from database
}

Then where you need to show:

session_start();
echo $_SESSION['logged_user'];
April 29, 2012

PHP UTF-8 Encoding in HTML Input Box

Question by Wasim

Here what is in database

Seilpendel für Tragsysteme

now i am loading it into textbox via AJAX but what loads into textbox is

Seilpendel f�r Tragsysteme

but i want correct string Seilpendel für Tragsysteme into inputbox

i have tried

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

but not its working

Answer by mcdado

Make sure the db is encoded in UTF8, or use utf8_encode() or utf8_decode() when loading data from db or when writing back.

Answer by Starx

The correct way solving this problem is to configure database to use UTF8 as their character encoding in and out.

RUN The following query after connecting to the database, one time and everything should start working.

SET NAMES 'utf8';
April 20, 2012

Prevent links from auto hyperlinking in Outlook etc using PHP

Question by Ben Carey

I know that this can be done using settings in Outlook, but that only sorts the issue for myself.

What I would like to do is use PHP to prevent text from being hyperlinked just because there is an @ sign etc…

As far as I can see, the only option for me is to encode all @ signs to their HTML numeric entity like so:

Something like this:

$message = str_replace('@','&#64;',$message);

However, if possible, I do not want this to happen if the @ sign is part of an email address.

Therefore I need something like this:

// SOME_REGEX will match any @ sign that is NOT part of an email address
$message = preg_replace('SOME_REGEX','&#64;',$message);

Can anybody think of any other better methods? Are there any flaws in this plan? Can anyone suggest a good regular expression for this? I am struggling to write a regex that matches an @ sign if it is not part of an email address

Thanks in advance

Answer by Anthony

This will not work if the email address is wrapped in anything not defined in the trim list.

$chunked_message = explode(" ", $message);

foreach($chunked_message as $chunk) {
    $clean_chunked_message[] = 
               (!filter_var(trim($chunk, " -().?!trn", FILTER_VALIDATE_EMAIL)) 
               ? str_replace('@', '&#64;' $chunk) : $chunk;
}

$clean_message = implode(" ", $clean_chunked_message);

Good luck.

Answer by Starx

This is a feature of mail application to detect link when it is found and make it click able.

A dirty trick to escape this situation is to use space in between the links.

Example:

http://ww w.you tube.com/v=.......
March 2, 2012

How to get OS default encoding?

Question by alex347

What is the proper way of getting default OS encoding? For Linux it can be found here: /etc/sysconfig/i18n

If you think the best way is to read from that file, then can I rely it will work on all modern major Linux distributions? What about Windows?

Answer by Starx

The best way to detect encoding, is from the piece of text you are trying to read from.

Use mb_detect_encoding()[docs here] function

$str = "....."; //use you own logic to get the text
echo mb_detect_encoding($str);

Adding on to @Evert

Encoding happens when characters are displayed on the screen or CLI interface. It is not OS dependent, rather content specific.

November 24, 2010

How to insert chinese character in mysql table?

Question by user319198

I want to save data in Chinese language in mysql table.

Can anyone tell me what settings I have to do for it in mysql and PHP.

My table will save data in both English and Chinese some column English other Chinese . Is it possible with single table.

Any help will be appreciated.

Answer by Guixing Bai

Use UTF-8 when you create table

create table xxx ()CHARACTER SET = utf8;

Use UTF-8 when you insert to table

set names utf8; insert into xxx (xx,x);

Answer by Starx

Set the character set to UTF-8, in mysql.

Check this for reference

http://confluence.jetbrains.net/display/TCD/Configuring+UTF8+Character+Set+for+MySQL
http://dev.mysql.com/doc/refman/5.0/en/charset-mysql.html

...

Please fill the form - I will response as fast as I can!