Jump to content

Login info trouble..


finkyfeke

Recommended Posts

My site has a register/login system which posts username & password info to a php file, which accesses a mysql database. In the database, there is a 'firstname' field.

The problem is that I'm trying to have "Welcome, John!" (or whatever is in the 'firstname' field) to display on the page.

I can get it to work on the first page after login, but after that it's as if it doesn't carry across the information, although to access these pages, you must still be logged in.

Below are my login.php and membershome.php scripts. You can see the problem yourself at http://www.stringtutor.co.uk and logging in as Username: test ; Password: test

Log in and it will say 'Welcome, John!", but type in membershome.php and it will miss out the "John".

Please help.

Thanks in advance.

 

login.php

<?php

//Database Information

$dbhost = "fdb1.awardspace.com";
$dbname = "stringtutor_db";
$dbuser = "stringtutor_db";
$dbpass = "********";

//Connect to database

mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());

session_start();
$username = $_POST['username'];
$password = md5($_POST['password']);

$query = "select * from users where username='$username' and password='$password'";

$result = mysql_query($query);

if (mysql_num_rows($result) != 1) {
$error = 'Bad Login';
    echo 'Wrong Username and/or Password';
    include 'login.html';

} else {
    $_SESSION['username'] = '$username';
    include 'membershome.php';
}

?>

 

 

membershome.php

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

$dbhost = "fdb1.awardspace.com";
$dbname = "stringtutor_db";
$dbuser = "stringtutor_db";
$dbpass = "********";

//Connect to database

mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());

// members page



if ( empty( $username ) ) {

    include 'notloggedin.html';

} else {

?>

<loads of html was above here, but it's irrelevent>
<div id="html1" style="position:absolute; overflow:hidden; left:323px; top:21px; width:476px; height:35px; z-index:9">
<div id="text4" style="position:absolute; overflow:hidden; z-index:9"><div class="wpmd">
<div><font class="ws12" face="Palatino Linotype">Welcome,

<?
$query = "select firstname from users where username='$username' limit 1";

$result = mysql_query($query);
$row = mysql_fetch_array($result);

echo ($row['firstname'])
?>
!</font></div>
</div></div></div>


</div></body>
</html>

<?
}
?>

Link to comment
Share on other sites

Try replacing $username in the memberhome.php query with $_SESSION[username].

 

<?php
//...change:
$query = "select firstname from users where username='$username' limit 1";
//...to:
$query = "select firstname from users where username='$_SESSION[username]' limit 1";
?>

 

Also, you could define a $_SESSION['firstname'] so you don't have to keep querying the database each time.

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.