Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6801781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:08:32+00:00 2026-05-26T19:08:32+00:00

I have this php page that posts to itself and then it checks weather

  • 0

I have this php page that posts to itself and then it checks weather if to login someone or not. The problem I am having is that if it logins… then it still shows the username and password textboxes.. but if i refresh they go away and now the welcome thing comes up thanks to the session.

What i want is to once the submit is clicked and it logs the person in to immediately not show the textboxes (username, password) and show the welcome message. Right now i have to refresh.

Please note i am new to PHP and any wise advise will be much appreciated.

    <?php 

        echo "<form method=\"post\" action=\"index.php?form_type=$page_vals\">";
        echo "<body>";

        //Start session
         session_start();

       //Check whether the session variable SESS_MEMBER_ID is present or not
       if(!isset($_SESSION['SESS_MEMBER_ID']) || (trim($_SESSION['SESS_MEMBER_ID']) == '')) {

         extract($_POST);

       $username = "";
       $password = "";
       $userrole = "";
       $userid ="";

       $login_query = "SELECT user_id, user_role, user_username FROM users WHERE user_username = '$_POST[logInUsername]' AND user_password = '$_POST[logInPassword]'";

        if(!($database = mysql_connect("localhost","root","")))
                die("<p>Could not connect to database</p></div></div>
                                 </body>
                                </html>");

            if(!mysql_select_db("mydatabase", $database))
                die("<p>Could not open my databases database</p></div>
               </div>
                                 </body>
                                </html>");

                if(!($result = mysql_query($login_query, $database)))
                {
                    print("Could not execute query!<br/>");
                    die(mysql_error()."</div>
               </div>            
                                 </body>
                                </html>");
                }


            if (mysql_num_rows($result) == 0) {
               print("Please verify your login information<br/>");
                }


            while ($row = mysql_fetch_assoc($result)) {
                  $username = $row["user_username"];
                  $userrole = $row["user_role"];
                  $userid = $row["user_id"];
              }

            echo "Hello - '$username'";
            mysql_close($database);

            session_regenerate_id();

            $_SESSION['SESS_MEMBER_ID'] = $userid;
            $_SESSION['SESS_NAME'] = $username;

            //Write session to disc
            session_write_close();

                   echo '<div id="login" class="login">
        <label for="login">User Name</label>
        <input type="text" name="logInUsername" />
        <label for="Password">Password</label>
        <input type="password" name="logInPassword" />
        <input type="submit" value="Submit" class="button" />
        </div>';
        }
   else 
   {
    $sessionName = $_SESSION['SESS_NAME'];
     echo '<div id="login" class="login">
        <label for="welcome">Welcome '. $sessionName.'!</label>
        </div>';
   }

    ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:08:32+00:00Added an answer on May 26, 2026 at 7:08 pm

    Problem here is just your code is not in sequence. I have corrected Try it now.

    <?php 
       session_start();
    
        echo "<body>";
    
        //Start session
      //print_r($_SESSION);exit;
    
       //Check whether the session variable SESS_MEMBER_ID is present or not
    
         extract($_POST);
    
       $username = "";
       $password = "";
       $userrole = "";
       $userid ="";
    if(isset($_POST))
    {
       $login_query = "SELECT reg_id, role_id, f_name FROM registration WHERE f_name = '$_POST[logInUsername]' AND password = '$_POST[logInPassword]'";
    
        if(!($database = mysql_connect("sunlinux","pukhraj","pukhraj123")))
                die("<p>Could not connect to database</p></div></div>
                                 </body>
                                </html>");
    
            if(!mysql_select_db("testbaj", $database))
                die("<p>Could not open my databases database</p></div>
               </div>
                                 </body>
                                </html>");
    
                if(!($result = mysql_query($login_query, $database)))
                {
                    print("Could not execute query!<br/>");
                    die(mysql_error()."</div>
               </div>            
                                 </body>
                                </html>");
                }
    
    
            if (mysql_num_rows($result) == 0) {
               print("Please verify your login information<br/>");
                }
    
    
            while ($row = mysql_fetch_assoc($result)) {
                  $username = $row["f_name"];
                  $userrole = $row["role"];
                  $userid = $row["reg_id"];
              }
    
             $_SESSION['SESS_MEMBER_ID'] = $userid;
            $_SESSION['SESS_NAME'] = $username;
       }
       if(!isset($_SESSION['SESS_MEMBER_ID']) || (trim($_SESSION['SESS_MEMBER_ID']) == '')) {
    
            echo "Hello - '$username'";
            mysql_close($database);
    
            session_regenerate_id();
    
    
    
            //Write session to disc
            session_write_close();
    
            echo "<form method=\"post\" ><div id=\"login\" class=\"login\">
            <label for=\"login\">User Name</label>
            <input type=\"text\" name=\"logInUsername\" />
            <label for=\"Password\">Password</label>
            <input type=\"password\" name=\"logInPassword\" />
            <input type=\"submit\" value=\"Submit\" class=\"button\" />
            </div>";
            }
       else 
       {
        $sessionName = $_SESSION['SESS_NAME'];
         echo "<div id=\"login\" class=\"login\">
            <label for=\"welcome\">Welcome '$sessionName' !</label>
            </div>";
       }
    
    ?>
    

    Small changes :

    1. Just plase form tag at appropriate place.
    2. Never mix code after post and before post.
    3. here all database stuff should be execute after submit so I enclosed them in condition if(isset($_POST))
    4. due to nonlinearity of code it was creating session after one more refresh after post data. Now corrected.

    for message :

    do below changes :

    1. give name to submit button <input type=\"submit\" name=\"submit\" value=\"Submit\" class=\"button\" />

    2. replace first if condition with if(isset($_POST['submit']))

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page that is accessed via a URL like this: http://power-coder.net/Test/something.php?id=3#Page1 I
i have tried this code to redirect a php page.but it s not working
I turned this case into a simple PHP page that submits to itself. My
I have a simple HTML form that posts to a PHP page, which will
I have a page in php that gets from the url such as this
I have a site with URLs like this: http://domain.co.uk/subdir/page.php I have redesigned the site
I have a web page coded in PHP. This page is for posting a
Say i have this PHP code: $FooBar = a string; i then need a
I am trying to create a web page using php. The problem is that
I am running php 5.3, and have bumped into a problem I've not encountered

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.