Forum Moderators: coopster

Message Too Old, No Replies

how to pass the user name using cookies

         

ravii

1:18 pm on Dec 29, 2008 (gmt 0)

10+ Year Member



hello,
I don't know how to use cookies

please help me in this

I am using the dream weaver cs4

I designed the navigation bar to my page using dream weaver cs4
navigation bar contains Home, Retail Contact Us

In my retail.php when user login he goes to the second.php
but i need to pass this user name using cookies to the second.php

the thing is when user successfully logged in he will be in the second.php now from second.php when the user again clicks on the Retail.php i.e retail tab in the navigation bar with out logging out
then it should display Welcome message instead the old

if the user logouts and clicks on the retail tab then it should display all the input fields again for the user to login newly again

hope u understand my doubt

here is my code

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<!--<script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
<link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />-->
<script type="text/javascript">
<!--
function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_nbGroup(event, grpName) { //v6.0
var i,img,nbArr,args=MM_nbGroup.arguments;
if (event == "init" && args.length > 2) {
if ((img = MM_findObj(args[2])) != null && !img.MM_init) {
img.MM_init = true; img.MM_up = args[3]; img.MM_dn = img.src;
if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
nbArr[nbArr.length] = img;
for (i=4; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
if (!img.MM_up) img.MM_up = img.src;
img.src = img.MM_dn = args[i+1];
nbArr[nbArr.length] = img;
} }
} else if (event == "over") {
document.MM_nbOver = nbArr = new Array();
for (i=1; i < args.length-1; i+=3) if ((img = MM_findObj(args[i])) != null) {
if (!img.MM_up) img.MM_up = img.src;
img.src = (img.MM_dn && args[i+2]) ? args[i+2] : ((args[i+1])? args[i+1] : img.MM_up);
nbArr[nbArr.length] = img;
}
} else if (event == "out" ) {
for (i=0; i < document.MM_nbOver.length; i++) {
img = document.MM_nbOver[i]; img.src = (img.MM_dn) ? img.MM_dn : img.MM_up; }
} else if (event == "down") {
nbArr = document[grpName];
if (nbArr)
for (i=0; i < nbArr.length; i++) { img=nbArr[i]; img.src = img.MM_up; img.MM_dn = 0; }
document[grpName] = nbArr = new Array();
for (i=2; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
if (!img.MM_up) img.MM_up = img.src;
img.src = img.MM_dn = (args[i+1])? args[i+1] : img.MM_up;
nbArr[nbArr.length] = img;
} }
}
//-->
</script>

<style type="text/css">
#container
{
float:left;
border:5px solid #666;
position:relative;
width:770px;
height:450px;
margin:20px auto 0 100px;
top:-20px;
}

</style>

</head>
<?php
function connect()
{
$dbhost='aaa';
$dbuser='aaa';
$dbpass='aaaaa';
$conn= mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'.mysql_error());
$dbname='aaaaa';
mysql_select_db($dbname);
return $conn;
}
function IsEmpty($cuser,$cpass)
{
$str1=strlen($cuser);
$str2=strlen($cpass);
$nRes=min($str1,$str2);
echo "$nres";
if ($nRes==0)
{
echo "<font size='3' face='Garamond' color='red'><b> Missing user Id / Password! </b></font> </br>";
}
return $nRes;
}
?>

<body bgcolor="#0a1936" onload="document.form1.cname.focus();""MM_preloadImages('/home_normal.jpg','/home_clicked_down.JPG','/retail_normal.JPG','/retail_clicked_down.JPG','/contact_clicked_down.JPG','/about_clicked_normal.JPG','/about_clicked_down.JPG')">

<table align="center" border="0" cellpadding="0" cellspacing="0">
<tr>

<td><a href="welcome.html" target="_top" onclick="MM_nbGroup('down','group1','Home','/home_normal.jpg',1)" onmouseover="MM_nbGroup('over','Home','/home_clicked_down.JPG','',1)" onmouseout="MM_nbGroup('out')"><img src="/home_normal.jpg" alt="" name="Home" width="87" height="33" border="0" id="Home" onload="" /></a></td>


<td><a href=<?php "retail.php?uname=".$_COOKIE['cook_user'] ?>target="_top" onclick="MM_nbGroup('down','group1','Retailers','/retail_normal.JPG',1)" onmouseover="MM_nbGroup('over','Retailers','/retail_clicked_down.JPG','',1)" onmouseout="MM_nbGroup('out')"><img src="/retail_normal.JPG" alt="" name="Retailers" width="113" height="33" border="0" id="Retailers" onload="" /></a></td>


<td><a href="contact_us.php" target="_top" onclick="MM_nbGroup('down','group1','contact','/contact_clicked_down.JPG',1)" onmouseover="MM_nbGroup('over','contact','/contact_clicked_down.JPG','',1)" onmouseout="MM_nbGroup('out')"><img src="/contact_normal.jpg" alt="" name="contact" width="134" height="32" border="0" id="contact" onload="" /></a></td>

</tr>
</table>

<div id="container">
<form method="post" action="<?php $_SERVER['PHP_SELF'] ?>" name="form1" id="form1">
<?php
$cuser1=$_GET['uname'];
if (!empty($cuser1))
{
echo "<font color='white'> Welcome to Hakman & CO... </font>";
}
else
{
?>
<label style="color:#FFFFFF">Email Id: </label>
<input type="text" name="cname" id="cname" height="14px" width="170px"/><br/>
<label style="color:#FFFFFF">Password:</label>
<input type="password" name="cpass" id="cpass" height="14px" width="177px"/>
<input type="image" id="checkme" name="checkme" src="/sign_in_off1.PNG" /><br/><br/><br/>

<?php
if (!empty($_POST))
{
$cusername=$_POST['cname'];
$cpassword=$_POST['cpass'];

setcookie ("cook_user", $cusername,time()+3600); /* expire in 1 hour */
$check=IsEmpty($cusername,$cpassword);
if ($check>0)
{
$c=connect();
$query = "SELECT user_id,password FROM users where user_id='".$cusername."' AND password='".$cpassword."'";
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
if (mysql_num_rows($result) > 0)
{
//echo"<font size='4' face='Garamond' color='white'><b>hello swetha</b></font></br>";
//header('Location:spry_styles.php');
echo "<script language='Javascript'>window.location='onclick_images.php?uname=$cusername';</script>";
}
else
{
echo "<font size='3' face='Garamond' color='white'><b>Oouchh.. you failed to Log In </b></font></br>";
}
}
}
}
?>

</form>
</div>

</body>
</html>

andrewsmd

3:05 pm on Dec 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I didn't look at your code, but your problem can be solved using session variables instead of cookies. Something like this pseudo
<?php
//you have to start a session on every page
session_start();

if(their login is correct){

//set a session variable
$_SESSION['user'] = $cusername;

}
?>

Now whenever they go to another page, you just put in the
session_start(); and then you can echo
$_SESSION['user'] wherever you want and it should have their username. As long as they keep their browser open it will save their username. You can then just use session_destroy(); if they click the logout button. However, if they close their browser and then open it again they will have to login again (which is how you should design your site anyways). Let me know if you need some more help, give me some code if you need syntax help.

jojy

11:27 pm on Dec 31, 2008 (gmt 0)

10+ Year Member



here is the cookie code

on login page use this
setcookie("user", 'ravii', time()+3600);

//on second page
if(isset($_COOKIE['user'])) {
echo 'Welcome ', $_COOKIE['user'];
}
else {
header("Location: page1.php");
exit;
}

hope this will help you