Log in

View Full Version : Resolved Echo username using SESSION from login page to redirected page?



Priyo
02-19-2012, 09:21 PM
Scenario:
Two pages - one login and another confirmation of login. I have used SESSION upon a successful login and also successfully redirected to the confirmation page. But I want to echo the username in the redirected page. Which I haven't been able to be successful.

Here is the code for my first and second page both.

Login page -

<?php
ob_start("ob_gzhandler");
include('config.php');

if($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = mysql_real_escape_string($_POST[Customer_Name]);
$password = mysql_real_escape_string($_POST[Password]);
//query
$query = mysql_query("SELECT * FROM customer WHERE Customer_Name='$name' AND Password='$password'");
$query_rows = mysql_num_rows($query);
if($query_rows > 0) {
echo("Successful Login!");
session_start();
$_SESSION['cname'] = $name;
header ("location: logged.php");

} else {
echo("Login Failed!");
}
}

?>

and confirmation page -

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<meta name="author" content="" />
<title>Colors Inc.</title>
<style type="text/css">
<!--
@import url("style.css");
-->
</style>
<script src="Scripts/AC_RunActiveContent.js" type="text/javascript"></script>

</head>

<body>
<div id="container">
<div id="header">
<h1>Colors Inc.</h1>
<p class="logo">Color Up Your Life</p>
</div>
<!-- menu -->
<div id="nav">
<ul>
<li><a href="index.html">Home</a> </li>
<li> <a href="products.html">Products</a></li>
<li> <a href="#">Login</a></li>
<li> <a href="#">Gallery</a></li>
<li> <a href="#">Contact Us</a> </li>
</ul>
</div>
<!-- content area -->
<div id="content" align="center">
<h1>Welcome</h1>

<p>&nbsp;</p>
<div align="center"><cite>
<script type="text/javascript">
AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0','width','431','height','110','title','animation2','src','ColorsFinal2','quality','high','pluginspage','http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash','movie','ColorsFinal2' ); //end AC code
</script>
<noscript>
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="431" height="110" title="animation2">
<param name="movie" value="ColorsFinal2.swf" />
<param name="quality" value="high" />
<embed src="ColorsFinal2.swf" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="431" height="110"></embed>
</object>
</noscript>
</cite></div>
<div class="horline"></div>
<h3><center>
<p>You have successfully logged in SESSION USERNAME SHOULD APPEAR HERE</p>
<p>Now you place your order by clicking <a href="order.php">HERE</a> </p>
</center></h3>

<p>&nbsp;</p>
<h3>Comment from a Customer:</h3>
<div class="box">
<p>I've been searching my desired poster for years! And finally it has become possible because of Colors Inc. This is just what I've wanted! Many thanks to them!</p>
<p>- Hamim Al Ahsan</p>
</div>

<p>And that's how our customers love us! Now feel free to roam around the site, choose a product of your choice, or upload your own design! You'll get it in your hands, the service which you didn't have all this time!</p>

<div class="horline"></div>
<h3>&nbsp;</h3>
<h3>Lists</h3>
<p>Some Flash Advertisement</p>

</div>
<!-- content area -->
<div id="footer">

<p class="center">
&nbsp;&nbsp;<a href="http://validator.w3.org/check/referer">XHTML</a> :: <a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a><br>
Copyright &copy; 2012 colorsinc.com.bd:: Design by Priyotosh Das </p>
</div>
</div>

</body>
</html>

Can anyone suggest how can I sustain the SESSION and echo the name on the redirected page?

keyboard
02-19-2012, 10:22 PM
You can only use sessions if you have started the session. Add this to the top of your page.

session_start();

Also, you have named your session variable cname so simply put this whereever you want to echo it.


echo $_SESSION['cname'];

so your code should look like this



<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<meta name="author" content="" />
<title>Colors Inc.</title>
<style type="text/css">
<!--
@import url("style.css");
-->
</style>
<script src="Scripts/AC_RunActiveContent.js" type="text/javascript"></script>

</head>

<body>
<div id="container">
<div id="header">
<h1>Colors Inc.</h1>
<p class="logo">Color Up Your Life</p>
</div>
<!-- menu -->
<div id="nav">
<ul>
<li><a href="index.html">Home</a> </li>
<li> <a href="products.html">Products</a></li>
<li> <a href="#">Login</a></li>
<li> <a href="#">Gallery</a></li>
<li> <a href="#">Contact Us</a> </li>
</ul>
</div>
<!-- content area -->
<div id="content" align="center">
<h1>Welcome</h1>

<p>&nbsp;</p>
<div align="center"><cite>
<script type="text/javascript">
AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0','width','431','height','110','title','animation2','src','ColorsFinal2','quality','high','pluginspage','http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash','movie','ColorsFinal2' ); //end AC code
</script>
<noscript>
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="431" height="110" title="animation2">
<param name="movie" value="ColorsFinal2.swf" />
<param name="quality" value="high" />
<embed src="ColorsFinal2.swf" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="431" height="110"></embed>
</object>
</noscript>
</cite></div>
<div class="horline"></div>
<h3><center>
<p>You have successfully logged in <?php echo $_SESSION['cname']; ?></p>
<p>Now you place your order by clicking <a href="order.php">HERE</a> </p>
</center></h3>

<p>&nbsp;</p>
<h3>Comment from a Customer:</h3>
<div class="box">
<p>I've been searching my desired poster for years! And finally it has become possible because of Colors Inc. This is just what I've wanted! Many thanks to them!</p>
<p>- Hamim Al Ahsan</p>
</div>

<p>And that's how our customers love us! Now feel free to roam around the site, choose a product of your choice, or upload your own design! You'll get it in your hands, the service which you didn't have all this time!</p>

<div class="horline"></div>
<h3>&nbsp;</h3>
<h3>Lists</h3>
<p>Some Flash Advertisement</p>

</div>
<!-- content area -->
<div id="footer">

<p class="center">
&nbsp;&nbsp;<a href="http://validator.w3.org/check/referer">XHTML</a> :: <a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a><br>
Copyright &copy; 2012 colorsinc.com.bd:: Design by Priyotosh Das </p>
</div>
</div>

</body>
</html>

Priyo
02-20-2012, 05:36 AM
Thank You So Much. It is working just as I expect. I was thinking almost nearly the same thing, just the placement was confusing me.

Thanks again keyboard1333. :D