Jump to content

login problem


mr_badger

Recommended Posts

I have created a login page that works fine, but on my site before you login it says welcome guest, when you login it is suppose to say welcome username. For some reason it just says welcome guest even when logged in.

This is the code on my main site

<?php
//This will start a session
session_start();

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

//Check do we have username and password

if(!$username && !$password){
echo "Welcome Guest! <br> <a href=login.php>Login</a> | <a href=register.php>Register</a>";
}else{
echo "Welcome ".$username." (<a href=logout.php>Logout</a>)";
}



?>

 

Link to comment
Share on other sites

 

<?php

session_start();

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

  if((isset($_SESSION['username'])) && (isset($_SESSION['password'])) {
    
  echo "Welcome ".$username." (<a href=logout.php>Logout</a>)";
  }

    else {
    echo "Welcome Guest! <br> <a href=login.php>Login</a> | <a href=register.php>Register</a>";
    }

?>

Link to comment
Share on other sites

<?php
session_start();

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

  if((isset($_SESSION['username'])) && (isset($_SESSION['password'])) {
    
  echo "Welcome ".$username." (<a href=logout.php>Logout</a>)";
  }

    else {
    echo "Welcome Guest! <br> <a href=login.php>Login</a> | <a href=register.php>Register</a>";
    }

?>

Link to comment
Share on other sites

Try...

 

<?php
session_start();

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

  if(isset($_SESSION['username']) && isset($_SESSION['password']) {
    
  echo "Welcome ".$username." (<a href=logout.php>Logout</a>)";
  }

    else {
    echo "Welcome Guest! <br> <a href=login.php>Login</a> | <a href=register.php>Register</a>";
    }

?>

Link to comment
Share on other sites

I'am now getting an error saying Parse error: syntax error, unexpected '{'

<?php
session_start();

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

  if(isset($_SESSION['username']) && isset($_SESSION['password']) {
    
  echo "Welcome ".$username." (<a href=logout.php>Logout</a>)";
  }

    else {
    echo "Welcome Guest! <br> <a href=login.php>Login</a> | <a href=register.php>Register</a>";
    }

?>

Link to comment
Share on other sites

Well this:

 

<?php
session_start();

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

  if(isset($_SESSION['username']) && isset($_SESSION['password'])) {
    
  echo "Welcome ".$username." (<a href=logout.php>Logout</a>)";
  }

    else {
    echo "Welcome Guest! <br> <a href=login.php>Login</a> | <a href=register.php>Register</a>";
    }

?>

 

Works fine for me. I wonder if you accidently changed the wrong thing?

Link to comment
Share on other sites

why do you have to use the complete initialization that whole thing you already assign then in a variables right?///

 

try this

 

 

<?php

session_start();

 

$username = $_SESSION['username'];

$password = $_SESSION['password'];

 

  if(isset($username) && isset($password))

{

   

  echo "Welcome ".$username." (<a href=logout.php>Logout</a>)";

  }

 

    else {

    echo "Welcome Guest! <br> <a href=login.php>Login</a> | <a href=register.php>Register</a>";

    }

 

?>

Link to comment
Share on other sites

Thanks for the replies, the last code works but I still have my original problem, it is still saying welcome guest instead of a username. Could the problem be where I have placed the code.

 

I have 2 pages, on the main page I call header through <?php require and that is where the login/register is. Maybe I'am placing the code wrong because everything is working with no errors.

 

this is the main page:

<?php
require("header.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<link rel="stylesheet" href="corestyle.css" type="text/css" />
<style type="text/css">
<!--
#Layer5 {
position:absolute;
width:200px;
height:25px;
z-index:1;
left: 137px;
top: 42px;
}
#Layer6 {
position:absolute;
width:200px;
height:115px;
z-index:2;
left: 137px;
top: 192px;
}
#Layer7 {
position:absolute;
width:200px;
height:115px;
z-index:3;
left: 137px;
top: 311px;
}
#Layer8 {
position:absolute;
width:200px;
height:115px;
z-index:4;
left: 137px;
top: 430px;
}
#Layer9 {
position:absolute;
width:200px;
height:115px;
z-index:5;
left: 137px;
top: 549px;
}
#music_button {
position:absolute;
width:200px;
height:98px;
z-index:1;
left: 9px;
top: 271px;
}
#video_button {
position:absolute;
width:200px;
height:106px;
z-index:2;
left: 241px;
top: 271px;
}
#artists_button {
position:absolute;
width:200px;
height:115px;
z-index:3;
left: 473px;
top: 271px;
}
#television_button {
position:absolute;
width:200px;
height:115px;
z-index:4;
left: 9px;
top: 466px;
}
#film_button {
position:absolute;
width:200px;
height:115px;
z-index:5;
left: 241px;
top: 466px;
}

#Layer10 {
position:absolute;
width:200px;
height:115px;
z-index:6;
}
#music_text {
position:absolute;
width:222px;
height:52px;
z-index:6;
left: 14px;
top: 394px;
}
.style2 {font-family: Verdana, Arial, Helvetica, sans-serif}
.style3 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 9px; }
#video_text {
position:absolute;
width:186px;
height:43px;
z-index:7;
left: 259px;
top: 395px;
}
#artist_text {
position:absolute;
width:193px;
height:38px;
z-index:8;
left: 488px;
top: 396px;
}
#tv_text {
position:absolute;
width:200px;
height:40px;
z-index:9;
left: 19px;
top: 590px;
}
#film_text {
position:absolute;
width:200px;
height:30px;
z-index:10;
left: 252px;
top: 590px;
}
.style4 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 9px; color: #C4BEAF; }
#box_thing {
position:absolute;
width:226px;
height:5px;
z-index:11;
background-color: #807C73;
left: 9px;
top: 260px;
}
#Layer11 {
position:absolute;
width:226px;
height:5px;
z-index:12;
left: 241px;
top: 260px;
background-color: #807C73;
}
#Layer12 {
position:absolute;
width:226px;
height:5px;
z-index:13;
left: 473px;
background-color: #807C73;
top: 260px;
}
#Layer13 {
position:absolute;
width:226px;
height:5px;
z-index:14;
background-color: #807C73;
left: 9px;
top: 455px;
}
#Layer14 {
position:absolute;
width:226px;
height:5px;
z-index:15;
background-color: #807C73;
left: 241px;
top: 455px;
}
#whats_on {
position:absolute;
width:458px;
height:115px;
z-index:16;
left: 9px;
top: 139px;
background-color: #807C73;
}
-->
</style>
</head>

<body>
<div id="box_thing"></div>

<div class="style4" id="film_text">Get the latest news and movie trailers in our film section. </div>
<div class="style4" id="tv_text">See what's coming up on music now with out tv guide plus get all the latest tv news. </div>
<div class="style4" id="artist_text">Visit the artists section to browse the artists webpages for bio, photo's and more. </div>
<div class="style4" id="video_text">Watch the latest music video online through our video player, we have selected all the current videos. </div>
<div id="television_button">
  <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="226" height="115">
    <param name="movie" value="television_button.swf" />
    <param name="quality" value="high" />
    <embed src="television_button.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="226" height="115"></embed>
  </object>
</div>

<div id="artists_button">
  <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="226" height="115">
    <param name="movie" value="artists_button.swf" />
    <param name="quality" value="high" />
    <embed src="artists_button.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="226" height="115"></embed>
  </object>
</div>

<div id="video_button">
  <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="226" height="115">
    <param name="movie" value="video_button.swf" />
    <param name="quality" value="high" />
    <embed src="video_button.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="226" height="115"></embed>
  </object>
</div>

<div id="music_button">
  <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="226" height="115">
    <param name="movie" value="music_button.swf" />
    <param name="quality" value="high" />
    <embed src="music_button.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="226" height="115"></embed>
  </object>
</div>

<div id="film_button">
  <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="226" height="115">
    <param name="movie" value="film_button.swf" />
    <param name="quality" value="high" />
    <embed src="film_button.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="226" height="115"></embed>
  </object>
</div>


<div class="style4" id="music_text">Listen and download all the latest music from our download center.They are all free but if you just want to listen then you can through our music player. </div>




<div id="Layer11"></div>
<div id="Layer12"></div>
<div id="Layer13"></div>
<div id="Layer14"></div>




</body>
</html>

 

this is the header php:

<?php
session_start();
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
<!--
body,td,th {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
color: #716F6A;
}
body {
background-color: #8C887F;
}
a {
font-size: 10px;
color: #94918B;
}
a:link {
text-decoration: none;
color: #C4BEAF;
}
a:visited {
text-decoration: none;
color: #E0DDD6;
}
a:hover {
text-decoration: underline;
color: #C4BEAF;
}
a:active {
text-decoration: none;
}
#logo {
position:absolute;
width:200px;
height:62px;
z-index:1;
left: 63px;
top: 50px;
}
#advert_1 {
position:absolute;
width:390px;
height:62px;
z-index:2;
left: 271px;
top: 50px;
}
#jump_menu {
position:absolute;
width:142px;
height:11px;
z-index:3;
left: 38px;
top: 139px;
}
#search {
position:absolute;
width:191px;
height:11px;
z-index:4;
left: 193px;
top: 139px;
}
#login_register {
    color:#C4BEAF;
position:absolute;
width:270px;
height:6px;
z-index:5;
left: 570px;
top: 170px;
}

#login_register a {
color:#C4BEAF;
text-decoration:none;
}

#login_register a:hover {
color:#D5D0C3;
text-decoration:underline;
}

.style1 {
background-color:#C4BEAF;
color:#8C887F;
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:9px;
border:thin #DD9B00;
}
.style2 {color: #C4BEAF}
#Layer1 {
position:absolute;
width:114px;
height:16px;
z-index:5;
left: 661px;
top: 134px;
}

#navcontainer ul { list-style-type: none;
margin: 0;
padding: 0;
margin-top:120px;
margin-left:20px;
}

#navcontainer ul li { display: inline; }

#navcontainer ul li a { text-decoration: none;
padding: .2em 1em;
color: #C4BEAF;
background-color: #77746C;
}

#navcontainer ul li a:hover
{
color: #67645E;
background-color: #DCD6C7;
}
#Layer2 {
position:absolute;
width:220px;
height:15px;
z-index:6;
left: 344px;
top: 171px;
}
#Layer3 {
position:absolute;
width:200px;
height:115px;
z-index:7;
left: 70px;
top: 231px;
}
-->
</style>
<script>
function goHere(item,targ)  {
theChoice = item.selectedIndex;
theName=item.name;
item.selectedIndex = 0;
theURL=item.options[theChoice].value;

if (theChoice != 0) window.open(theURL,targ);
}
</script>
</head>

<body>
<div id="login_register">

<?php
$username = $_SESSION['username'];
$password = $_SESSION['password'];

  if(isset($_SESSION['username']) && isset($_SESSION['password'])) {
    
  echo "Welcome ".$username." (<a href=logout.php>Logout</a>)";
  }

    else {
    echo "Welcome Guest! <br> <a href=login.php>Login</a> | <a href=register.php>Register</a>";
    }

?>
</div>
<div id="logo">

<img src="main_logo.png" /></div>

<div id="advert_1"></div>

<div id="navcontainer">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Music</a></li>
<li><a href="#">Videos</a></li>
<li><a href="#">Listen</a></li>
<li><a href="#">News</a></li>
<li><a href="#">TV</a></li>
<li><a href="#">Film</a></li>
<li><a href="#">Artists</a></li>
<li><a href="#">Contact</a></li>
<li><a href="#">Downloads</a></li>
</ul>
</div>

<div id="Layer2">Search: 
  <label>
  <input type="text" name="textfield" />
  Go</label>
</div>
</body>
</html>

Link to comment
Share on other sites

Yes The problem is with initialization of sessions , I have it now now saying welcome username when I log in but How do I have peoples username?

  <?php
$username = $_SESSION['username'] = username;
$password = $_SESSION['password'] = password;

  if(isset($username) && isset($password))
{
   
  echo "Welcome ".$username." (<a href=logout.php>Logout)";
  }

    else {
    echo "Welcome Guest!
<a href=login.php>Login | <a href=register.php>Register";
    }

?>

Link to comment
Share on other sites

First, any reason why you're not closing your anchor tags? Haven't read the other posts...so not sure if there's a practical point to that. Second, You're defining the session variable for username, as "username"...that's why it's outputting that.

Link to comment
Share on other sites

another problem that I just found out is that when I click logout it goes to the logout page where you can go to the index or login again, when I click the index it goes to the index but it doesn't allow the user to login, it should say welcome guest login | register but it doesnt.

  <?php
$username = $_SESSION['username'] = username;
$password = $_SESSION['password'] = password;

  if(isset($username) && isset($password))
{
   
  echo "Welcome ".$username." (<a href=logout.php>Logout)";
  }

    else {
    echo "Welcome Guest!
<a href=login.php>Login | <a href=register.php>Register";
    }

?>

Link to comment
Share on other sites

Let's break this down in a lot more simple steps, for the sake of understanding what's going on.

 

<?php

  //Define the variables $username & $password

  $username = $_POST['username']; //Assuming you're posting from a form where
  $password = $_POST['password']; //the fields are 'username' & 'password'

    if(isset($_POST['username']) && isset($_POST['password'])) {

      session_start();

      //If the form has been submitted, then initialize the session and define the session variables
      $_SESSION['username'] = $username;
      $_SESSION['password'] = $password;

      echo 'Welcome '.$username.' (<a href="logout.php">Logout</a>)';
    }

      else {
        echo' Welcome Guest!<br /> <a href="login.php">Login</a> | <a href="register.php">Register</a>';
      }

?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.