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 8768669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:07:37+00:00 2026-06-13T17:07:37+00:00

I am a php newbie and I am using MAC OS. I recently set

  • 0

I am a php newbie and I am using MAC OS. I recently set up a website and test it under localhost. It works fine until one day I tried to rename my home folder, and quit halfway when switching from root back to the one that I have renamed. Then I logged out from the root user and loggin in the one that I have renamed. Then I found that I have to reset everything. The application folder had been moved outside the user home dir to the same level with the user folder.

Here’s the problem(I don’t know whether it is caused by the above action.)
I let a user log in with username and password, and use a session variable to store the username. I check with:

echo $_SESSION['name'];    

echo session_status();   

The first one shows the username I have input, and the second one prints 2
So I think that the username was stored in that session variable.
But then whenever the user enter another page, the session variable is destroyed.

What is the problem?

Here’s the code that I write for every page as page header. It contains the log in box. It used to work fine.

    <?php
    echo '<div id="pageheader">
            <div class="wrap">
                <div class="logo">
                    <a href="index.php"><img src="pics/1.gif" height="45px"></a>
                 </div>
                  <ul id="nav">
                    <li><a href="">New Arrival&nbsp&nbsp&nbsp/</a></li>
                    <li><a href="">Hot&nbsp&nbsp&nbsp/</a></li>
                    <li><a href="#">By School&nbsp&nbsp&nbsp/</a>
                        <ul >
                            <li><a href="">ADM</a></li>
                            <li><a href="">CEE</a></li>
                            <li><a href="">EEE</a></li>
                            <li><a href="">HSS</a></li>
                            <li><a href="">MAE</a></li>
                            <li><a href="">MSE</a></li>
                            <li><a href="">NBS</a></li>
                            <li><a href="">SBS</a></li>
                            <li><a href="">SCBE</a></li>
                            <li><a href="">SCI</a></li>
                            <li><a href="">SPMS</a></li>
                            <li><a href="">SCE</a></li>
                        </ul>           
                    </li>
                    <li><a href="#">By Course&nbsp&nbsp&nbsp/</a>
                        <ul>
                            <li><a href="book.php?q=Mathematics">Mathematics</a></li>
                            <li><a href="book.php?q=Computer">Computing</a></li>
                            <li><a href="book.php?q=Accounting">Accounting</a></li>
                            <li><a href="book.php?q=Business">Business Law</a></li>
                            <li><a href="book.php?q=Physics">Physics</a></li>
                            <li><a href="book.php?q=Chemistry">Chemistry</a></li>
                            <li><a href="book.php?q=Biology">Biology</a></li>
                        </ul>
                    </li>
                    <li><a href="#">By Kind&nbsp&nbsp&nbsp/</a></li>
                  </ul>
                <ul id="log">';

    function generateLogin(){
        echo '      <li id="login">
                        <a id="login-trigger" href="#">login&nbsp<span>&#x25BC</span></a>
                            <div id="login-content">
                                <form action="'.$_SERVER['PHP_SELF'].'" method="post" enctype="multipart/form-data">
                                    <fieldset class="inputs">
                                        <input name="user" type="text" placeholder="Username" class="reqd" />
                                        <input name="loginpw" type="password" placeholder="Password" class="reqd" />
                                    </fieldset>
                                    <fieldset class="actions">
                                        <input type="submit" class="button1" value="Log in" />
                                        <label><input name="keep" type="checkbox" value="yes" />Keep me signed in</label>
                                    </fieldset>
                                </form>
                            </div>
                    </li>
                    <li id="signup">
                        <a id="signup-trigger" href="#">signup&nbsp<span>&#x25BC</span></a>
                            <div id="signup-content">
                                <form action="signup.php" method="post" enctype="multipart/form-data">
                                    <fieldset class="inputs">
                                        <input name="username" id="username" type="text"  placeholder="Username (at least 6 characters)" class="reqd user" />
                                        <input name="email" id="email" type="email"  placeholder="john@example.com" class="reqd email" />
                                        <input id="passwd1" type="password" placeholder="Password (at least 6 characters)" class="reqd pd" />
                                        <input name="pswd" id="passwd2" type="password"  placeholder="Confirm your password" class="reqd passwd1" />
                                    </fieldset>
                                    <fieldset class="actions">
                                        <input type="submit" class="button1" value="Sign up" />&nbsp;&nbsp;&nbsp;<input type="reset" class="button1" />
                                    </fieldset>
                                </form>
                            </div>
                    </li>
    ';
    }

    session_start();

    if(!isset($_SESSION['name']) or $_GET['logout'] == 1){
        if($_GET['logout'] == 1){unset($_SESSION['name']);}
        if(isset($_POST['user'])){
            $link = mysqli_connect('localhost','root','root','bookstore'); 
            if (!$link){
              die('Could not connect: ' . mysqli_error());
            }
            try {
                $sql = "SELECT username FROM user
                    WHERE username='$_POST[user]' AND
                    pswd=MD5('$_POST[loginpw]')";

                $sqlUser = "SELECT username FROM user
                WHERE username='$_POST[user]'";

                $result1 = mysqli_query($link, $sql);
                $result2 = mysqli_query($link, $sqlUser);
                if (!mysqli_fetch_row($result1)){
                    if(!mysqli_fetch_row($result2)){
                        throw new Exception("User ".$_POST['user']." does not exist!");
                    }
                    else{
                        throw new Exception("Incorrect password!");
                    }
                }
                else{
                    $_SESSION['name'] = $_POST['user'];
                    echo '
                        <ul id="log">
                            <li><a href="index.php?logout=1">Logout</a></li>
                            <li><a href="cart.php">Hi, '.$_POST['user'].'</a></li>
                        </ul>';
                }
            }catch(Exception $e){
                echo "<script type='text/javascript'>alert('".$e->getMessage()."');</script>";
                generateLogin();
            }       
            mysqli_close($link);
        }
        else{
            generateLogin();
        }
    }
    else{
        $name = $_SESSION['name'];
        echo '
            <ul id="log">
                <li><a href="index.php?logout=1">Logout</a></li>
                <li><a href="cart.php">Hi, '.$_SESSION['name'].'</a></li>
            </ul>';
    }
    echo '          </ul>
                <div id="search">
                  <form action="book.php" name="search" method="get" enctype="multipart/form-data">
                      <input name="q" id="q" autocomplete="off" onkeyup="showHint(this.value)" type="text" size="100" placeholder="Search..." />
                      <input type="submit" class="button1" value="Search" />
                  </form>
                </div>
                <div id="suggestBox">
                  <ul>
                    <div id="txtHint">
                    </div>
                  </ul>
                </div>
            </div>
        </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-06-13T17:07:38+00:00Added an answer on June 13, 2026 at 5:07 pm

    You have to start the session on every page you use sessions

    <?php
        session_start();
    
        // now I can use my sessions in this page and 
        // I should start the session on each page I use them
    ?>
    

    You should use session_start() before any html output so in your case move session_start() before the first echo!

    <?php
        // move session_start here
    
        session_start();
    
        echo '<div id="pageheader">
                  <div class="wrap">';
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good day! I am a newbie in php.. I made a form using html
I'm newbie to PHP and I dont know much about database. I have one
I'm a newbie using a book to learn the Yii php framework. It has
I am a newbie with zend. On the web-site i am building using zend/php,
my project is using coneigniter, lightweight php framework. I am a little bit newbie
I am newbie using jquery mobile with php. I have a little problem on
PHP newbie here. I'm currently developing a shopping cart web site using PHP and
I am newbie to php.I have coded auto-complete text box using php,and i have
I newbie in PHP on Ubuntu, My computer already installed OpenERP and it using
I'm still a newbie in php and I'm using codeigniter for my backend framework.

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.