Jump to content

[SOLVED] on main page is welcomes users but its the wrong user


berry05

Recommended Posts

i have a index page and on that index page it welcomes a user but its always user tyler...here's the php code block that shows the user name...i don't know why it does that either..

 

<?php 

mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("textgame") or die(mysql_error());



$query = "SELECT * FROM users"; 

$result = mysql_query($query) or die(mysql_error());


$row = mysql_fetch_array($result) or die(mysql_error());
echo $row['username'];
?>

 

I think you need cookies for this to work mate, you can't just expect the database to remeber whos connecting. Try setting a cookie for the username and checking it against the database, then echoing it out in the welcome message.

 

Hope thanks helps.

 

Timecatcher.

Where's your login script? In it, do you set a cookie or session variable that contains the user's id or username?

 

 

i use sessions...here's the login script...

 

<?php
ob_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="textgame"; // Database name
$tbl_name="users"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$username = (isset($_POST['username'])) ? mysql_real_escape_string($_POST['username']) : FALSE;
$password1 = (isset($_POST['password1'])) ? $_POST['password1'] : FALSE;
$password2 = (isset($_POST['password2'])) ? $_POST['password2'] : FALSE;

if(!$username) die('username not set');
if(!$password1) die('Password1 not set');
else $password1 = md5($password1);
if(!$password2) die('Password2 not set');
else $password2 = md5($password2);

$query = "SELECT * FROM users WHERE username='$username' AND password1='$password1' AND password2='$password2'";
$result = mysql_query($query) or die( mysql_error());
$count=mysql_num_rows($result);
if($count==1){

session_register("username");
$_SESSION['username'] = $username;

header("location:index2.php");
}
else {
echo "Wrong Username or Password";
}

ob_end_flush();
?>

so if it is in the session all you have to do is echo out he username

 

echo "Welcome " . $_SESSION['username'];

 

but on your index page you will have to start the session. in order to do that you will need to put this right below the <?php:

 

session_start();

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.