logo-dw

Go Back   Dreamweaver Club Forums > Hand Coders Forum > PHP
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 08-05-2013, 11:55 AM   #1
prashanth
 
Join Date: Aug 2013
Posts: 3
Default Display username after login from login page in other pages

i want to display the username in other pages when i logged in to the login page & its redirect into other page. The redirected page should display the username.

In the login page it compare the username & password from the database, if it mach then only it redirect into other pages. i did all the necessary validation part to login page.
I am new to PHP. Please help me .

Please find the code below
index.php(Containing Login details)

Logic Part

<?php
session_start();
$myusername=$_SESSION;

if(isset($_POST['Submit']))
{

$myusername=trim($_POST["myusername"]);
$mypassword=trim($_POST["mypassword"]);

if($myusername == "" )
{
$error= "Error : You did not enter a username.";
$code= "1" ;
}

elseif($mypassword == "" )
{
$error= "Error : You did not enter a password.";
$code= "2" ;
}
else
{
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="transport"; // Database name
$tbl_name="members"; // Table name

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

// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1)
{
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
$_SESSION['myusername'] = $myusername;
}
else
{
$error= "Wrong Username or Password.";
}
}
}
?>

Body Part

<div class="row3" align="center">
<form name= "info" id= "info" method= "post" action= "" >
<table width= "327" border= "0" align="center" cellpadding= "5" cellspacing= "1" class="table">
<tr>
<td align="right">UserName: </td>
<td><input name= "myusername" type= "text" id= "myusername" placeholder="Username" value="<?php if(isset($myusername)){echo $myusername;} ?>"<?php if(isset($code) && $code == 4){echo "class=error" ;}?> ></td>
</tr>

<tr>
<td align="right">Password: </td>
<td><input name= "mypassword" type= "password" id= "mypassword" placeholder=" Password" value="<?php if(isset($mypassword)){echo $mypassword;} ?>"<?php if(isset($code) && $code == 5){echo "class=error" ;}?> ></td>
</tr>
<tr>
<td>&nbsp;&nbsp;</td>
<td><input type= "submit" name= "Submit" value= "Submit" /></td>
</tr>
</table>
</form>
<div><table align="center">
<?php if (isset($error)) { ?>
<tr>
<td colspan="2" align="center" ><?php echo "<p class='message'>" .$error. "</p>" ; ?></td>
</tr>
<?php } ?>
</table></div>


I want to display the username in the other page. Please give me code to disply.
I know that it will wil be done through using session.
Please help me..
prashanth is offline   Reply With Quote
Old 08-06-2013, 12:54 AM   #2
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,164
Default

$myusername=$_SESSION; theres you have the clue. if the new pages has session_start(); at the top of the page you can access the session name
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas
edbr is offline   Reply With Quote
Old 08-06-2013, 05:19 AM   #3
prashanth
 
Join Date: Aug 2013
Posts: 3
Default

Thank you for spending some time for me.
if i login with different username name, but it display username only Array.
dont no where i am going wrong.
prashanth is offline   Reply With Quote
Old 08-06-2013, 06:59 AM   #4
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,164
Default

yes Sessions is an array. if you want to see the sessions set
use
Code:
 <?php 
 session_start(); 
 Print_r ($_SESSION);
 ?>
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas
edbr is offline   Reply With Quote
Old 08-12-2013, 10:58 AM   #5
jc920060
Banned
 
Join Date: Aug 2013
Posts: 2
Default

<form method="post" action="">

<input type="text" name="username" />
<input type="password" name="password" />
<input type="submit" name="search" />
</form>

<?php
include('config.php');
if(isset($_REQUEST['search']))
{
$username= $_REQUEST['username'];
$query = "select * from user where user='"$username" ";
$res1 = mysql_query($query);
while($rows = mysql_fetch_array($res1))
{
?>

<?PHP echo $username; ?>

if is it incorrect please reply me correct one.
Thank you happy to help you.

Last edited by gentleone; 08-12-2013 at 03:28 PM.. Reason: deleted signature
jc920060 is offline   Reply With Quote
Old 08-12-2013, 03:31 PM   #6
gentleone
gentleone's Avatar
 
Join Date: Nov 2009
Location: Rhodes, Greece
Posts: 2,837
Default

jc920060, one more time posting a link signature underneath your posts and you're out. Read the forum rules!
__________________
www.gentlemedia.nl
gentleone is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 06:50 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Copyright 2006 DreamweaverClub.com