Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Php

I am trying to make a website i started learning php.. I just want it to login using sessions i did it and here tis the code

<?php ob_start(); include "functions.php"; // Login Password for testing purposes

define('USERNAME', 'testaccount'); define('PASSWORD', '123456');

if($_SERVER[REQUEST_METHOD] === POST ) {

$username = $_POST['username']; $password = $_POST['password'];

if (verify_function($username,$password)) { header('Location: welcome.php'); $_SESSION['username'] = $username; } else { $invalid = 'Wrong Login/pass'; }}?>

When someone is logged... it will go to welcome.php...

Is it some how possible rather then redirecting to another another page.. i can have the user on the same page index.php but instead of login button it says welcome user!? so all other pages have it?

Please help me

When the user logs in you can have a $_SESSION variable notate that they are "logged in". Then do something like:

if ($_SESSION['logged_in'] == TRUE) {

   echo "Welcome";
}

else {
   echo "Log In";

Fill in those if/else statements with different headers, footers or whatever you want. Hopefully that helps give some direction. It's something I use regularly in my projects.

1 Answer

Same suggestion as Peter above, but just add another '=' before TRUE. You will obviously need to set $_SESSION['logged_in'] at some point, probably within verify_function?

if ($_SESSION['logged_in'] == TRUE) {
   echo "Welcome";
}

else {
   echo "Log In";
}

Good catch.