Jump to content

Few Questions


shadowayex

Recommended Posts

Is there any way to have things from the fields in my database display on my page automatically. Like lets say a field had names in it. Is there a way to make that name display automatically if that person signs in? And I have it figured out how to sign people in, but how do you make it so the page knows WHO specifically is signed in?

Link to comment
Share on other sites

Could you explain it a bit more? From what i understand is that you want to display things from a db when a user signs in. If thats the case, then yes, it is possible. you just use SELECT * from tablename SQL statements and the mysql_fetch_assoc() function to create an array with column_name=value pairs. you then just echo out the values(echo $array['column_name'])

Link to comment
Share on other sites

The way I always do it is just to set a session variable to the user's id, and the script checks that when it needs a user-specific action.

Link to comment
Share on other sites

Ok, I have a log in page made that I learned in an old post. It works. The deal is I want each page to recognize WHO is logged in, and right now, my testing just confirms that someone IS logged in but not WHO. Here's the code from login.php:

<?php$link = mysql_connect('host', 'username', 'password')    or die('Could not connect: ' . mysql_error());mysql_select_db('database') or die('Could not select database');$username = mysql_real_escape_string($_POST['username']);$password = sha1($_POST['password']);$result = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'");if ($username == "" || $password == ""){   echo "";}else{  if (mysql_num_rows($result) == 1)   {     $_SESSION['logged_in'] = true;  }  else   {     echo "Error: Incorrect username or password!";  }}if ($username == "" || $password == ""){   echo "";}else{  if (isset($_SESSION['logged_in']))  {     header('location: home.php');  }  else  {     echo "";  }}mysql_close($link);?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /><title>Log In</title></head><body><div><form action="login.php" method="post"><div>Username: <input type="text" name="username" value="" size="20" /><br />Password: <input type="password" name="password" value="" size="20" /><br /><input type="submit" name="submit" value="Login" /></div></form></div></body></html>

Now, I'm sure there are errors and it's more than likely a nightmare to all of you advanced people :) but it works. I had an idea to use the $username variable to recognize the user each page, but if PHP is anything like Javascript, the variables are cleared as soon as you leave the page. So I have no idea how to do it. I thought about a cookie, but then that makes a nightmare for people who have cokies disabled, and honestly I have no idea how to do anything with cookies. What should I do?

Link to comment
Share on other sites

Create a session when the login equals true.if (mysql_num_rows($result) == 1) { $_SESSION['logged_in'] = true; $_SESSION['user'] = $username; }//////////////////////////////////Then welcome them with something like this, i hope it works cause i haven't tested it, but should give you an idea.<?php session_start();$user=$_SESSION['user'];if(isset($user)) echo "Welcome $user";?>

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...