Jump to content

Cookie Recall


superted27

Recommended Posts

Hi again all,

 

I wondered if anyone could help again (sorry).  I have one single page which I would like to record a cookie when the user hits submit. The next time the user visits the site, I want it to recall the cookie and put the value in the textbox on the page. I think I have it storing the cookie (not sure though), but I am having trouble getting it to recall in the text box.  I wondered if someone could look at this code and see where I am going wrong or help with what I need to add.  New to PHP so having trouble getting my head around things.  Thanks in advance.

 

<? setcookie('user',$userName,(time()+60*60*24*30) ); ?>
<style type="text/css">


<!--
body,td,th {
color: #FFFFFF;
}
body {
background-color: #000000;
}
.style1 {font-family: Verdana, Arial, Helvetica, sans-serif}
-->


</style><form id="form1" name="form1" method="post" action="">
  <label>
  <div align="center" class="style1"><strong>My Timetable Search</strong><br />
    <br />
    Enter Network User ID:<br />
    <input type="text" name="txtUserID" id="txtUserID"  value=""/>
  </div>
  <p align="center">
    <label>
    <input type="submit" name="cmdGo" id="cmdGo" value="Show Timetable" onclick="setcookie('user',$userName,(time()+60*60*24*30);"/>
    </label>
    <br />
  </p>
  <hr />
</form>
<div align="center">
  <?php


$userName = $_POST["txtUserID"];
$hostname = "xxxxx";
$dbuser = "xxxxx";
$dbpassword = "xxxxxx";
$dbname = "xxxxx";
$LessonCount = 0;

     $db_link=mysql_connect($hostname, $dbuser, $dbpassword)
     or die("Unable to connect to the server!");

     mysql_select_db($dbname)
     or die("Unable to connect to the database");

        $fields_array=array();
        $num_fields=0;
        $num_row=0;
       

$sql= "SELECT core.UserID, wstt.Period, wstt.Lesson, wstt.Staff, wstt.Room FROM wstt INNER JOIN core ON wstt.Adno = core.Adno WHERE core.UserID='$userName'";


    

// find position of "FROM" in query
        $fpos=strpos($sql, 'from');

        // get string starting from the first word after "FROM"
        $strfrom=substr($sql, $fpos+5, 50);

        // Find position of the first space after the first word in the string
        $Opos=strpos($strfrom,' ');

        //Get table name. If query pull data from more then one table only first table name will be read.
        $table=substr($strfrom, 0,$Opos);

           // Get result from query
            $result=mysql_query($sql);
            $num_row=mysql_numrows($result);

            print('<html>');
            print('<head><title>');
		  print('Timetable for'.$userName.'</title>');
            print('<link rel="stylesheet" href="style.css">');

            print("</head>");
            print('<body><br>');
		print('<h3>Timetable for: '.$userName.'</h3>');
		print('<div align="center"');


            if($num_row >0)
            {
                    //Get number of fields in query
                    $num_fields=mysql_num_fields($result);

         

           # get column metadata
            $i = 1;

             //Set table width 10% for each column
            $width=10 * $num_fields;

           // print('<br><table width='.$width.'% align="center" border=1> padding=0<tr>');
		print('<br><table width='.$width.'% border="1" cellpadding="0" cellspacing="0" bordercolor="#000066"<tr>');
            print('<tr><th colspan='.$num_fields.'>Timetable List</th></tr>');




             while ($i < $num_fields)
             {

              //Get fields (columns) names
            $meta = mysql_fetch_field($result);

            $fields_array[]=$meta->name;

           //Display column headers in upper case
       print('<th><b>'.strtoupper($fields_array[$i]).'</b></th>');

                    $i=$i+1;
                    }

            print('</tr>');
                


                   //Get values for each row and column
                while($row=mysql_fetch_row($result))
                {
                 print('<tr>');

			  


					//SET TO 1 TO HIDE USERNAME
                        for($i=1; $i<$num_fields; $i++)
                        {

                        //Display values for each row and column
                        print('<td>'.$row[$i].'</td>');



                        }


					//START
					if ($LessonCount ==4) {
						print('<tr>');
						print('<td colspan="4"><div align="center">-----------------------------------------------------</td></tr>');
						$LessonCount=0;
						}else {
						($LessonCount = $LessonCount+1);
						}


					//STOP	


                print('</tr>');
                }


    }


		print('</div>');


    ?>
</div>

Link to comment
Share on other sites

Hi,

 

Had another re-shuffle around based on what you said, but I don't think its working at all.  This is the entire page code nothing before.  All I need it to do is store the username which is entered so the next time they visit the page they don't need to enter it again.

 

Anyone be able to tell me or show me where I am going wrong? I am trying to figure this out myself but I am driving blind now  :-\

 

<?php
setcookie('userTT',$userName,(time()+60*60*24*30));

//FROM HERE

     print('<html>');
            print('<head><title>');
		  print('Timetable for'.$userName.'</title>');
		  
		  print('<style type="text/css">');
			print('body,td,th {');
			print('color: #FFFFFF;');
			print('}');
			print('body {');
			print('	background-color: #000000;');
			print('}');
			print('.style1 {font-family: Verdana, Arial, Helvetica, sans-serif}');
            print('<link rel="stylesheet" href="style.css">');

            print("</head>");
            print('<body><br>');
		echo "here";
echo $_COOKIE["userTT"];


print('</style><form id="form1" name="form1" method="post" action="">');
print('<label>');
  print('<div align="center" class="style1"><strong>My Timetable Search</strong><br />');
   print('<br />');
    print('Enter Network User ID:<br />');
    print('<input type="text" name="txtUserID" id="txtUserID"  value=""/>');
  print('</div>');
  print('<p align="center">');
    print('<label>');
    print('<input type="submit" name="cmdGo" id="cmdGo" value="Show Timetable"');
   print(' </label>');
   print(' <br />');
  print('</p>');
  print('<hr />');
print('</form>');
print('<div align="center">');

$userName = $_POST["txtUserID"];
$hostname = "xxxx";
$dbuser = "xxxxxxr";
$dbpassword = "xxxxx";
$dbname = "xxxxxx";
$LessonCount = 0;



     $db_link=mysql_connect($hostname, $dbuser, $dbpassword)
     or die("Unable to connect to the server!");

     mysql_select_db($dbname)
     or die("Unable to connect to the database");

        $fields_array=array();
        $num_fields=0;
        $num_row=0;
       

$sql= "SELECT core.UserID, wstt.Period, wstt.Lesson, wstt.Staff, wstt.Room FROM wstt INNER JOIN core ON wstt.Adno = core.Adno WHERE core.UserID='$userName'";


    

// find position of "FROM" in query
        $fpos=strpos($sql, 'from');

        // get string starting from the first word after "FROM"
        $strfrom=substr($sql, $fpos+5, 50);

        // Find position of the first space after the first word in the string
        $Opos=strpos($strfrom,' ');

        //Get table name. If query pull data from more then one table only first table name will be read.
        $table=substr($strfrom, 0,$Opos);

           // Get result from query
            $result=mysql_query($sql);
            $num_row=mysql_numrows($result);

       
		print('<h3>Timetable for: '.$userName.'</h3>');
		print('<div align="center"');


            if($num_row >0)
            {
                    //Get number of fields in query
                    $num_fields=mysql_num_fields($result);

         

           # get column metadata
            $i = 1;

             //Set table width 10% for each column
            $width=10 * $num_fields;

           // print('<br><table width='.$width.'% align="center" border=1> padding=0<tr>');
		print('<br><table width='.$width.'% border="1" cellpadding="0" cellspacing="0" bordercolor="#000066"<tr>');
            print('<tr><th colspan='.$num_fields.'>Timetable List</th></tr>');




             while ($i < $num_fields)
             {

              //Get fields (columns) names
            $meta = mysql_fetch_field($result);

            $fields_array[]=$meta->name;

           //Display column headers in upper case
       print('<th><b>'.strtoupper($fields_array[$i]).'</b></th>');

                    $i=$i+1;
                    }

            print('</tr>');
                


                   //Get values for each row and column
                while($row=mysql_fetch_row($result))
                {
                 print('<tr>');

			  


					//SET TO 1 TO HIDE USERNAME
                        for($i=1; $i<$num_fields; $i++)
                        {

                        //Display values for each row and column
                        print('<td>'.$row[$i].'</td>');



                        }


					//START
					if ($LessonCount ==4) {
						print('<tr>');
						print('<td colspan="4"><div align="center">-----------------------------------------------------</td></tr>');
						$LessonCount=0;
						}else {
						($LessonCount = $LessonCount+1);
						}


					//STOP	


                print('</tr>');
                }


    }


		print('</div>');




    ?>
</div>

Link to comment
Share on other sites

Use a browser plugin to display the headers on your requests. Ensure the cookie is in there.

 

Also, I didn't see you attempt to dump your $_COOKIE array anywhere. You should apply the advice given before asking for more.

Link to comment
Share on other sites

Thanks for that.

 

I tried print_r - nothing displayed so I took it out.

 

I don't know what a browser plugin is.  I am trying but advice given is rather generic so I can't actually see where I am going wrong.  I have looked at a lot of examples.

 

I know many people just post to forums to get an answer and be lazy, but I have spent all week trying to figure this out myself and I am at a loss now as I do not see what I am doing wrong?

 

For instance, am I collecting the cookie at the right point so it stores when the user enters a value?

 

Can I write something which detects a cookie and puts the value back in the text box on this same page? If so, how would I go about doing that?

 

 

Link to comment
Share on other sites

You're dealing with server->client interaction. Since PHP is ONLY on the server side, and since it's impossible for us to replicate the way your client is behaving, we can only tell you how to debug.

 

You don't know what a browser plugin is? That's okay, thankfully, you can spoon-feed yourself via Google!

http://www.google.com/search?q=what+is+a+browser+plugin

 

And, say you're using Google Chrome, you can find a plugin that'll show you headers quite easily

http://www.google.com/search?q=examine+headers+google+chrome

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.