By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,172 Members | 1,751 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,172 IT Pros & Developers. It's quick & easy.

how to pass the user name using cookies

P: 5
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

Expand|Select|Wrap|Line Numbers
  1.  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>Untitled Document</title>
  7. <!--<script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
  8. <link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />-->
  9. <script type="text/javascript">
  10. <!--
  11. function MM_preloadImages() { //v3.0
  12.  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
  13.   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
  14.   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
  15. }
  16.  
  17. function MM_findObj(n, d) { //v4.01
  18.  var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
  19.   d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  20.  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  21.  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  22.  if(!x && d.getElementById) x=d.getElementById(n); return x;
  23. }
  24.  
  25. function MM_nbGroup(event, grpName) { //v6.0
  26.  var i,img,nbArr,args=MM_nbGroup.arguments;
  27.  if (event == "init" && args.length > 2) {
  28.   if ((img = MM_findObj(args[2])) != null && !img.MM_init) {
  29.    img.MM_init = true; img.MM_up = args[3]; img.MM_dn = img.src;
  30.    if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
  31.    nbArr[nbArr.length] = img;
  32.    for (i=4; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
  33.     if (!img.MM_up) img.MM_up = img.src;
  34.     img.src = img.MM_dn = args[i+1];
  35.     nbArr[nbArr.length] = img;
  36.   } }
  37.  } else if (event == "over") {
  38.   document.MM_nbOver = nbArr = new Array();
  39.   for (i=1; i < args.length-1; i+=3) if ((img = MM_findObj(args[i])) != null) {
  40.    if (!img.MM_up) img.MM_up = img.src;
  41.    img.src = (img.MM_dn && args[i+2]) ? args[i+2] : ((args[i+1])? args[i+1] : img.MM_up);
  42.    nbArr[nbArr.length] = img;
  43.   }
  44.  } else if (event == "out" ) {
  45.   for (i=0; i < document.MM_nbOver.length; i++) {
  46.    img = document.MM_nbOver[i]; img.src = (img.MM_dn) ? img.MM_dn : img.MM_up; }
  47.  } else if (event == "down") {
  48.   nbArr = document[grpName];
  49.   if (nbArr)
  50.    for (i=0; i < nbArr.length; i++) { img=nbArr[i]; img.src = img.MM_up; img.MM_dn = 0; }
  51.   document[grpName] = nbArr = new Array();
  52.   for (i=2; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
  53.    if (!img.MM_up) img.MM_up = img.src;
  54.    img.src = img.MM_dn = (args[i+1])? args[i+1] : img.MM_up;
  55.    nbArr[nbArr.length] = img;
  56.  } }
  57. }
  58. //-->
  59. </script>
  60.  
  61. <style type="text/css">
  62.               #container
  63.               {
  64.                 float:left;
  65.                 border:5px solid #666; 
  66.                 position:relative; 
  67.                 width:770px; 
  68.                 height:450px; 
  69.                 margin:20px auto 0 100px;
  70.                 top:-20px;
  71.               }             
  72.  
  73.               </style>    
  74.  
  75. </head>
  76. <?php
  77.       function connect()
  78.       {
  79.        $dbhost='aaa';
  80.        $dbuser='aaa';
  81.        $dbpass='aaaaa'; 
  82.        $conn= mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'.mysql_error());
  83.     $dbname='aaaaa';
  84.        mysql_select_db($dbname);
  85.  return $conn;
  86.       }
  87. function IsEmpty($cuser,$cpass)
  88. {
  89. $str1=strlen($cuser);
  90. $str2=strlen($cpass);
  91. $nRes=min($str1,$str2);
  92. echo "$nres";
  93. if ($nRes==0)
  94. {
  95.  echo "<font size='3' face='Garamond' color='red'><b> Missing user Id / Password! </b></font> </br>";
  96. }  
  97. return $nRes;
  98. }
  99. ?>
  100.  
  101. <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')">
  102.  
  103. <table align="center" border="0" cellpadding="0" cellspacing="0">
  104. <tr>
  105.  
  106. <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>
  107.  
  108.  
  109.   <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>
  110.  
  111.  
  112.   <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>
  113.  
  114. </tr>
  115. </table>
  116.  
  117. <div id="container">
  118. <form method="post" action="<?php $_SERVER['PHP_SELF'] ?>" name="form1" id="form1">
  119. <?php
  120. $cuser1=$_GET['uname'];
  121. if (!empty($cuser1))
  122. {
  123. echo "<font color='white'> Welcome to Hakman & CO... </font>";
  124. }
  125. else
  126. {
  127. ?>
  128. <label style="color:#FFFFFF">Email Id: </label>
  129. <input type="text" name="cname" id="cname" height="14px" width="170px"/><br/>
  130. <label style="color:#FFFFFF">Password:</label>
  131. <input type="password" name="cpass" id="cpass" height="14px" width="177px"/>
  132. <input type="image" id="checkme" name="checkme" src="/sign_in_off1.PNG" /><br/><br/><br/>
  133.  
  134.  <?php
  135. if (!empty($_POST))
  136. {
  137. $cusername=$_POST['cname'];
  138. $cpassword=$_POST['cpass'];
  139.  
  140. setcookie ("cook_user", $cusername,time()+3600); /* expire in 1 hour */ 
  141. $check=IsEmpty($cusername,$cpassword);
  142. if ($check>0)
  143. {
  144. $c=connect(); 
  145. $query = "SELECT user_id,password FROM users where user_id='".$cusername."' AND password='".$cpassword."'";
  146. $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 
  147. if (mysql_num_rows($result) > 0)
  148. {                                
  149. //echo"<font size='4' face='Garamond' color='white'><b>hello swetha</b></font></br>";
  150.  //header('Location:spry_styles.php'); 
  151.  echo "<script language='Javascript'>window.location='onclick_images.php?uname=$cusername';</script>"; 
  152. }
  153. else 
  154. {
  155. echo "<font size='3' face='Garamond' color='white'><b>Oouchh.. you failed to Log In </b></font></br>";
  156. }
  157. }
  158. }
  159. ?>
  160.  
  161. </form>
  162. </div>
  163.  
  164. </body>
  165. </html>
  166.  
  167.  
Dec 29 '08 #1
Share this Question
Share on Google+
3 Replies


xNephilimx
Expert 100+
P: 213
It would be pretty extensive to explain that here, your answer comes right out a book or the php site, learn how to use sessions:
PHP: Sessions - Manual
Dec 29 '08 #2

Atli
Expert 5K+
P: 5,058
I wrote an article about how to use session a while back.
Perhaps that can help you.

http://bytes.com/topic/php/insights/644923-php-sessions
Dec 29 '08 #3

P: 22
Never mind, i didnt read the 2nd post
Jan 3 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.