Hello,

I am trying to create a members page for my website and I want it to display a welcome message for when the person views it, something like:

"Welcome <USERNAME>"

I am new to PHP so I am not sure if there is something I am doing wrong, here is what I have so far:

For the Login Page

<?php
    session_start();

    $Connection = mysql_connect("localhost","root","password") or die ("Login Fail");
    mysql_select_db("database") or die ("Cannot Find DB");

    if(isset($_SESSION['Logged']))
    {
        die(header ('Location: ../Pages/Members.php'));
    }

        $Username   = strip_tags(strtolower($_POST['Username']));
        $Password   = strip_tags(md5($_POST['Password']));

       $mysql = mysql_query("SELECT * FROM Member_Data WHERE Username = '$Username' AND Password = '$Password'");
       if(mysql_num_rows($mysql) < 1)
       {
         die(header('Location: Errors/Password.php'));
       } 

       $_SESSION['Logged'] = "YES";
       $_SESSION['Username'] = $Username;

       die(header('Location: ../Pages/Members.php'));


?>

(I have removed my password and DB name from the code for safety reasons, but I know for a fact these are correct).

And this is the part for displaying the message:

<?php

    session_start();

    {
        echo $_SESSION['Username'] or die (mysql_error());
    }

?>

When I try and echo it out, all I get is 'Welcome 1'.
Originally I thought this was the ID in the database, because I was the first account I had that ID but when creating another account I still get the same message.

Please could someone tell me what I am doing wrong and how to fix it, thanks!

Recommended Answers

All 4 Replies

I think your first script is ok but, you can check by placing print_r($_POST); exit; right after session_start() and see what you get from your form.

Then, in your second script you have the curly brackets without any condition and or die(mysql_error()); but I don't see any query. Try to remove them.

@Cereal

Thanks for the reply, I have added the print_r($_POST); exit; to my code and it displayed my Username and password but I am still getting '1' instead of the username.

And in terms of the or die(mysql_error()); don't I need it as I am saying to kill the script if it fails and to display a piece of text. In this case the mysql error?

Thanks again and thanks in advanced for any more help.

@Cereal

Thanks, it works now I have removed the or die(mysql_error());

Glad you solved :)

And in terms of the or die(mysql_error()); don't I need it as I am saying to kill the script if it fails and to display a piece of text. In this case the mysql error?

You can use mysql_error() if you have a query in that same script, but usually you set this right after a query, not in the echo. As you did for mysql_connect() and mysql_select_db(). Bye!

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.