0

On my index page what I'm trying to achieve is after login a dropdown button on the top right of my page to change from "Account" to "Welcome [User]" and the buttons inside to change from "Login" to "View Account" & "Log out" I have the PHP code that says "Welcome [User]" but I'm not too sure on how to switch them out.

<?php 
    session_start(); 
?>

<!DOCTYPE html>
<html lang="en">
<script type="text/javascript" src="jquery-3.2.1.min.js"></script>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>    
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">

<head>
    <title>Bootstrap Case</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="">
    <meta name="author" content="">

    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <link rel="icon" href="../../favicon.ico">

    <!-- Bootstrap core CSS -->
    <link href="../../dist/css/bootstrap.min.css" rel="stylesheet">

    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <link href="../../assets/css/ie10-viewport-bug-workaround.css" rel="stylesheet">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

    <title>Carousel Template for Bootstrap</title>

    <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
    <!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
    <script src="../../assets/js/ie-emulation-modes-warning.js"></script>

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!-- Custom styles for this template -->
    <link href="carousel.css" rel="stylesheet">
</head>
<!-- NAVBAR ================================================== -->

<body style="height:1500px">
    <nav class="navbar navbar-inverse navbar-fixed-top">
        <div class="container-fluid">
            <div class="navbar-header">
                <a class="navbar-brand" href="#">Jobsite</a>
            </div>
        <div>
        <ul class="nav navbar-nav" style="display: inline-block;">
            <li class="active"><a href="#">Home</a></li>
            <li><a href="#">Who are we?</a></li>
            <li><a href="#">Make a resume</a></li>
            <li><a href="#">Search for jobs</a></li>
            <li><a href="#">Profile</a></li>

            <li class="dropdown">
                <a class="dropdown-toggle" data-toggle="dropdown" href="#"><b>
                <!-- logged in user information -->
                <?php  if (isset($_SESSION['username'] )): ?>
                    <p>Welcome <strong><?php echo $_SESSION['username']; ?></strong></p>
                    <p>
                        <ul class="dropdown-menu">
                            <a href="index.php?logout='1'" style="color: red;">logout</a> </p>
                            <li><a href="http://localhost/registration/login.php">Login</a></li>
                            ...
                <?php endif ?>
                ...
            ...
        </div>
    </b>

//-----------------
//  Etc...
//-------------------

I've seen(and attempted with no luck) str_replace(). I'm not sure how the best way to do this is.

Server.php

<?php 
session_start();

// variable declaration
$username = "";
$email    = "";
$errors = array(); 
$_SESSION['success'] = "";

// connect to database
$db = mysqli_connect('localhost', 'root', '', 'registration');

// REGISTER USER
if (isset($_POST['reg_user'])) {
    // receive all input values from the form
    $username = mysqli_real_escape_string($db, $_POST['username']);
    $email = mysqli_real_escape_string($db, $_POST['email']);
    $password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
    $password_2 = mysqli_real_escape_string($db, $_POST['password_2']);

    // form validation: ensure that the form is correctly filled
    if (empty($username)) { array_push($errors, "Username is required"); }
    if (empty($email)) { array_push($errors, "Email is required"); }
    if (empty($password_1)) { array_push($errors, "Password is required"); }

    if ($password_1 != $password_2) {
        array_push($errors, "The two passwords do not match");
    }

    // register user if there are no errors in the form
    if (count($errors) == 0) {
        $password = md5($password_1);//encrypt the password before saving in the database
        $query = "INSERT INTO users (username, email, password) 
                  VALUES('$username', '$email', '$password')";
        mysqli_query($db, $query);

        $_SESSION['username'] = $username;
        $_SESSION['success'] = "You are now logged in";
        header('location: index.php');
    }

}

// ... 

// LOGIN USER
if (isset($_POST['login_user'])) {
    $username = mysqli_real_escape_string($db, $_POST['username']);
    $password = mysqli_real_escape_string($db, $_POST['password']);

    if (empty($username)) {
        array_push($errors, "Username is required");
    }
    if (empty($password)) {
        array_push($errors, "Password is required");
    }

    if (count($errors) == 0) {
        $password = md5($password);
        $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
        $results = mysqli_query($db, $query);

        if (mysqli_num_rows($results) == 1) {
            $_SESSION['username'] = $username;
            $_SESSION['success'] = "You are now logged in";
            header('location: index.php');
        }else {
            array_push($errors, "Wrong username/password combination");
        }
    }
}

?>

4
  • what error you faced ? Commented Oct 12, 2017 at 8:20
  • the str_replace didn't really have an error it was more the fact that i could only figure out how to replace text with text not with another php code (i believe due to the brackets its placed in?) because <?php if (isset($_SESSION['username'] )): ?> has to somehow fit in- <?php echo str_replace("world","Peter","Hello world!"); ?> (or something like that) Commented Oct 12, 2017 at 8:29
  • Can you add your php code. Especially the part where you set the session? Commented Oct 12, 2017 at 9:40
  • I added the server.php one (but there is also- user.php, errors.php, login.php, and registration.php.) Commented Oct 12, 2017 at 9:57

2 Answers 2

1

You can change the whole dropdown content using php, so it shows what you want on each case. Something like this (adapt the content of each option to your case)...

<li class="dropdown">
  <a class="dropdown-toggle" data-toggle="dropdown" href="#"><b>

  <?php if (isset($_SESSION['username'] )) { ?>
    <p>Welcome <strong><?=$_SESSION['username']?></strong></p>
    <p><a href="index.php?viewaccount=<?=$_SESSION['username']?>" style="color: red;">logout</a></p>
    <p><a href="index.php?logout='1'" style="color: red;">logout</a></p>
  <?php } else { ?>
    <p><a href="http://localhost/registration/login.php">Login</a></p>
  <?php } ?>

You'll have to tweak it a little bit to use the html element distribution you prefer, but I hope you understand the idea. Just create all the dropdown content for each case.

I hope it helps

Sign up to request clarification or add additional context in comments.

Comments

0

Ok got it thanks to A. Iglesias's code-

     <li class="active"><a href="#">Home</a></li>
    <li><a href="#">Who are we?</a></li>
    <li><a href="#">Make a resume</a></li>
    <li><a href="#">Search for jobs</a></li>
    <li><a href="#">Profile</a></li>

  <?php if (isset($_SESSION['username'] )) { ?>
 <li class="dropdown">
  <a class="dropdown-toggle" data-toggle="dropdown" href="#"><b>
 <p>Welcome <strong><?=$_SESSION['username']?></strong></p>
   <ul class="dropdown-menu">
   <a href="index.php?logout='1'" style="color: red;">logout</a> </p>
 </b>
  </a>
  </div>
  <?php } else { ?>
  <li class="dropdown">
  <a class="dropdown-toggle" data-toggle="dropdown" href="#"><b>
  <p>Account</p>
  <ul class="dropdown-menu">
  <p><a href="http://localhost/registration/login.php">Login</a></p>
  <?php } ?>

</ul>
</li>
</ul>
</div>
</div>
</nav>

so now If a user is logged in it will show "Welcome [User]" in a drop-down bar (which includes the log out and soon to be account page) and when no one is logged in it shows "Account" which has a drop down to a log in page. Thanks Guys!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.