ryanmetzler3 Posted August 18, 2013 Share Posted August 18, 2013 I will show you my code real quick to start: Here is the registration form and script. <?php include ('config.php'); if ($_SERVER['REQUEST_METHOD'] == 'POST') { $username = mysql_real_escape_string($_POST['username']); $password = mysql_real_escape_string(md5($_POST['password'])); $firstname = mysql_real_escape_string($_POST['firstname']); $lastname = mysql_real_escape_string($_POST['lastname']); $email = mysql_real_escape_string($_POST['email']); if (empty($username)) { echo("You have to fill in an username!"); } else { if(empty($password)){ echo ("You have to fill in a password!"); } else { $query = mysql_query("SELECT * FROM users WHERE username = '$username'"); $rows = mysql_num_rows($query); } if ($rows > 0) { die("Username taken!"); } else { $user_input = mysql_query("INSERT INTO users (username, password, firstname, lastname, email) VALUES ('$username','$password','$firstname','$lastname','$email')"); echo("Succesfuly Registered!"); } } } ?> <html> <head> <title>Register</title> </head> <body> <form action="register.php" method="post"> First Name: <input type="text" name="firstname"><br/> Last Name: <input type="text" name="lastname" /><br/> Email: <input type="text" name="email" /><br/> Username: <input type="text" name="username" /><br/> Password: <input type="password" name="password"/><br/> <input type="submit" value="Register!"/> </form> </body> </html> Here is the login form and script: <?php include ("config.php"); if ($_SERVER['REQUEST_METHOD'] == 'POST') { $username = mysql_real_escape_string($_POST['username']); $password = mysql_real_escape_string(md5($_POST['password'])); $query = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'"); $query_rows = mysql_num_rows($query); if($query_rows > 0) { $user_data = mysql_fetch_array($query); echo ("Succesfull Login!"); session_start(); $_SESSION['user'] = "$user_data"; } else { echo ("Username and/or password incorrect"); } } if (isset($_SESSION['user'])) { echo "Welcome {$_SESSION['user']['username']}"; } ?> <html> <head> <title>Login</title> </head> <body> <form action="login.php" method="post"> Username: <input type="text" name="username" /><br/> Password: <input type="password" name="password"/><br/> <input type="submit" value="Login!"/> </form> </body> </html> I am having two problems. When I hit login the very last if statement from the login code is supposed to echo a welcome message back out to the user with their username. But no matter the username it always just says "Welcome A". I am totally stumped on that. That is my first question. Here is my other general question. Here is my site s-w-a-p-e-z-e-e(dot)com (take the junk out, I just don't want this showing up in search engines). When a person logs in I hope to be able to display their username above the picture in the top right of my site pages. Also when they upload a photo I want their username to be injected into the database with their photo. I am guessing you need to use sessions to do this. I am teaching myself to program so be kind haha. Thanks so much anyone who can help! Link to comment https://forums.phpfreaks.com/topic/281289-user-sign-in-and-sessions/ Share on other sites More sharing options...
PravinS Posted August 20, 2013 Share Posted August 20, 2013 session_start() function should be at the top every time Link to comment https://forums.phpfreaks.com/topic/281289-user-sign-in-and-sessions/#findComment-1445934 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.