So the article title says it. i wanna make the name as like a welcome message, something like "Welcome, <name>!" and yes there are many articles on the web that gives you coding and what not but i still don't understand how to get it done. sigh. forgive me im a noob at this php mysql stuff.
Anyway, my login page uses email and password:

loginpage.php

<form name="flogin" action="index.php" method="post">
    <table width="350px">
        <tr>
        <td ><b>Email :</b></td>
        <td><input  type="text" name="email"></td>
        </tr>
        <tr>
        <td><b>Password :</b></td>
        <td><input  type="password" name="password"></td>
        </tr>
    </table>
    <div><input type="submit" class="login" value="Login" name="login"></div> 
</form>

<?php
session_start();

if (isset($_POST['login']))
{
    $email = mysql_real_escape_string($_POST['email']);
    $password = mysql_real_escape_string($_POST['password']);

    $email=stripslashes($email);
    $password=stripslashes($password);

    if ($email&&$password)
    {
        require "connect.php";
        $query = mysql_query("SELECT * FROM register WHERE email='$email'");        
        $numrows = mysql_num_rows($query);

        if ($numrows != 0)
        {   
            while ($rows = mysql_fetch_assoc($query))
            {
                $dbemail = $rows['email'];
                $dbpassword = $rows['password'];
            }
            if ($email==$dbemail && $password==$dbpassword)
            {
                $_SESSION['email']=$dbemail;
                echo ('<script type="text/javascript">window.location = "profile.php"</script>');
            }
            else 
            {
                echo ('<script type="text/javascript">alert("Incorrect Password");
                window.location = "index.php";
                </script>');
            }
        }
        else 
        {
            echo ('<script type="text/javascript">alert("The Email Does Not Exist In Database");
            window.location = "index.php";</script>');
        }
    }
    else 
    {
        echo ('<script type="text/javascript">alert("Please Enter An Email and/or Password");
                window.location = "index.php";</script>');
    }
}
exit();
?>

now what do i do so that after login and user goes to profile on the profile page it says "Welcome, <users name>!" like facebook where we login with email and password and our name is on our profile and that particular coding, do i put it in the profile.php or login.php ?

Recommended Answers

All 4 Replies

Member Avatar for ialzein
<?php
session_start();
if (isset($_POST['login'])) {
    $email = mysql_real_escape_string(isset($_POST['email']) ? stripslashes($_POST['email']) : false);
    $password = mysql_real_escape_string(isset($_POST['password']) ? stripslashes($_POST['password']) : false);
    ## REMOVED ## $email = stripslashes($email);
    ## REMOVED ## $password = stripslashes($password);
    if ($email && $password) {
        require "connect.php";
        $query = mysql_query("SELECT * FROM register WHERE email LIKE '$email'"); //I am supposing you have id, name, email and password fields in your 'register' table
        $numrows = mysql_num_rows($query);
        if (1 == $numrows) { // the email address must be unique in your table in mysql. so if it exits it must exist only ONCE
            $row = mysql_fetch_assoc($query);
            ## REMOVED ## while ($rows = mysql_fetch_assoc($query)) {
            ## REMOVED ##   $dbemail = $rows['email'];
            ## REMOVED ##   $dbpassword = $rows['password'];
            ## REMOVED ## }
            if ($email == $row['email'] && $password == $row['password']) {
                $_SESSION['email'] = $row['email'];
                $_SESSION['name'] = $row['name'];
                echo ('<script type="text/javascript">window.location = "profile.php"</script>');
            } else {
                echo ('<script type="text/javascript">alert("Incorrect Password");
                window.location = "index.php";
                </script>');
            }
        } else {
            echo ('<script type="text/javascript">alert("The Email Does Not Exist In Database");
            window.location = "index.php";</script>');
        }
    } else {
        echo ('<script type="text/javascript">alert("Please Enter An Email and/or Password");
                window.location = "index.php";</script>');
    }
    exit();
}
?>
<form name="flogin" action="index.php" method="post">
    <table width="350px">
        <tr>
            <td ><b>Email :</b></td>
            <td><input  type="text" name="email"></td>
        </tr>
        <tr>
            <td><b>Password :</b></td>
            <td><input  type="password" name="password"></td>
        </tr>
    </table>
    <div><input type="submit" class="login" value="Login" name="login"></div> 
</form>

If you follow ibrahim.alzein great revision, I'd replace any echo('<script... window.location = pageurl.php ...');. No reason to use JS for the redirects. Replace with:

header("Location: pageurl.php");

Adding to the post above, now on your welcome page the first thing should be session_start();, and you can thereafter access the user's name with $_SESSION['name'].

wow thank you so much, you gave the whole coding. i really appreciate it! :D but i got this error Parse error: syntax error, unexpected 'if' (T_IF) in C:\xampp\htdocs\CashFlow\index.php on line 18

?

and this is on my profile.php did i do it right?

<?php
session_start();

require "connect.php";
if($_SESSION['name'])
{
    $name =  $_SESSION['name'];
    $query =  mysql_query("SELECT name FROM register WHERE name ='$name'");
    $numrows = mysql_num_rows($query);

    if(1 == $numrows)
    {
        while ($rows = mysql_fetch_assoc($query))
        {
            echo "Welcome, ".$rows['name'];."!"; 
        }
    }
}

?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.