display username after logging in

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

ianhaney
New php-forum User
New php-forum User
Posts: 27
Joined: Tue May 01, 2012 12:30 pm

display username after logging in

Postby ianhaney » Sun Nov 11, 2012 6:32 am

Hi

I really need some help with PHP

I am trying code something like the following

After a user logs in, I want a page to display his username, how do I do that

On the page where I want to display the username, I have got the following PHP coding so far

Code: Select all

<?php
//set vars
$user = $_POST['user'];
$pass = md5($_POST['pass']);

if ($user&&$pass)
{
//connect to db
$connect = mysql_connect("$hostname","$username","$password") or die("not connecting");
mysql_select_db("databasename") or die("no db :'(");
$query = mysql_query("SELECT * FROM $tablename WHERE username='$user'");
?>


My login php page coding is below

Code: Select all

<?php
//set vars
$user = $_POST['user'];
$pass = md5($_POST['pass']);

if ($user&&$pass)
{
//connect to db
$connect = mysql_connect("$hostname","$username","$password") or die("not connecting");
mysql_select_db("databasename") or die("no db :'(");
$query = mysql_query("SELECT * FROM $tablename WHERE username='$user'");

$numrows = mysql_num_rows($query);


if ($numrows!=0)
{
//while loop
  while ($row = mysql_fetch_assoc($query))
  {
    $dbusername = $row['username'];
    $dbpassword = $row['password'];
  }
  else
      die("incorrect username/password!");
}
else
  echo "user does not exist!";
}
else
    die("please enter a username and password!");
?>
<br>
<div id="login">
Enter username and password below to login
</div>
<form id='login' action='checklogin.php' method='post' accept-charset='UTF-8'>
<fieldset>
<legend></legend>
<input type='hidden' name='submitted' id='submitted' value='1'/>
<label for='username' >Username*:</label>
<input type='text' name='username' id='username'  maxlength="50" />
<label for='password' >Password*:</label>
<input type='password' name='password' id='password' maxlength="50" />
<input type='submit' name='Submit' value='Login' />
</fieldset>
</form>
<br><br>
<div id="forgot">
Enter your registered email address below to generate a new password
</div>
<form method="post" action="forgotpassword.php">
<label for="email">Email:</label>
<input type="text" title="Please enter your email address" name="email" size="30"/>
<input type="submit" value="Submit" class="submit-button"/>
</form>


My registration form coding is below

Code: Select all

<?php
//=============Configuring Server and Database=======
$host        =    'hostname';
$user        =    'username';
$password    =    'password';
//=============Data Base Information=================
$database    =    'databasename';
 
$conn        =    mysql_connect($host,$user,$password) or die('Server Information is not Correct'); //Establish Connection with Server
mysql_select_db($database,$conn) or die('Database Information is not correct');
 
//===============End Server Configuration============
 
//=============Starting Registration Script==========
 
$email    =    $_POST['email'];

$username    =    $_POST['txtUser'];
 
$password    =    $_POST['txtPassword'];
 
//=============To Encrypt Password===================
$password    =    md5($salt.$password);
//============New Variable of Password is Now with an Encrypted Value========
 
if(isset($_POST['btnRegister'])) //===When I will Set the Button to 1 or Press Button to register
{
$query    =    "insert into users(email,username,password)values('$email','$username','$password')";
$res    =    mysql_query($query);
header('location:registersuccess.html');
}
 
?>

<?php
$to = "$email";
$subject = "Registration Details";
$message = "Email: $email \n Username: $username \n Password: $password";
$from = "myemailaddress";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>

MeroD
New php-forum User
New php-forum User
Posts: 53
Joined: Wed Oct 10, 2012 12:14 am

Re: display username after logging in

Postby MeroD » Sun Nov 11, 2012 10:27 pm

First:
session_start();
$_SESSION['user'] = $_POST['user'];

Other pages:
session_start();
echo $_SESSION['user'];

abdul
New php-forum User
New php-forum User
Posts: 5
Joined: Sat Aug 25, 2012 3:52 am

Re: display username after logging in

Postby abdul » Mon Nov 12, 2012 12:38 pm

You need to specify the section in your page where you want the name of the user to be displayed, a simple echo statement would do

ianhaney
New php-forum User
New php-forum User
Posts: 27
Joined: Tue May 01, 2012 12:30 pm

Re: display username after logging in

Postby ianhaney » Mon Nov 12, 2012 1:03 pm

Hi MeroD

bit lost on the following

Where do I put the following, I see you got first, is that as in the very first page of the website and or first as in at the top

First:
session_start();
$_SESSION['user'] = $_POST['user'];

Other pages:
session_start();
echo $_SESSION['user'];

Hi abdul

I want the name of the user to be displayed at the top of the webpage

Hope that helps

Ian

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: display username after logging in

Postby seandisanti » Mon Nov 12, 2012 2:12 pm

Yes, you want your session_start as the first line. jream.com has a quick video primer on php sessions that is well worth the 6.5 minutes required to follow along from start to finish.
http://jream.com/learning/videos/php-ba ... e-security


Return to “PHP coding => General”

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest

cron