Results 1 to 24 of 24

Thread: random variable

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    366

    random variable

    Hi
    I have made this little piece of code, that saves a cookie called cookies, I want the value of cookie to be the value of the random quote (the page puts up a random quote on loading, from the text file quotes.txt) But if i put the setcookie line under the randomquote line, i get an error about header changing. I dont even know if i can do what i have done with the value of the cookie being $randomquote. Please can someone help me?
    Thanks
    Alex
    Code:
    <?php
    setcookie ("cookies", $randomQuote);
    include("oneline_random_quote.php");
    randomQuote("quotes.txt");
    
    ?>
    <head>
    <body>
    <p>
    <?
    echo $_COOKIE["cookies"];
    ?>
    </body>
    </head>

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: random variable

    you can't set a cookie after headers have been sent to the browser. headers are sent implicitly after sending any text to the browser, or explicitly when you actually use the header() function to send a header of your choice.

    basically, what this means is that you need to set the cookie before you do anything in the script (besides include() statements that include scripts that send to output -- ie: configuration/class files).

  3. #3
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: random variable

    You are better off using sessions for that. None the less, you still need to ensure session_start() is called before all output, as that too sends a cookie:
    PHP Code:
    <?php
    session_start
    ();

    if(! isset(
    $_SESSION['quote']))) {
        
    $_SESSION['quote'] = randomQuote("quotes.txt");
    }

    $randomQuote $_SESSION['quote'];
    ?>
    <html>
      <head>
        <title>Page Title</title>
      </head>
      <body>
        <p>
          <?php echo($randomQuote); ?>
        </p>
      </body>
    </html>
    Also, make sure you use valid mark-up. <p> cannot appear in the <head> element.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    366

    Re: random variable

    i thas an error
    Code:
    Fatal error: Call to undefined function: randomquote() in /home/virtual-web/ksd-design/public/other/cookie.php on line 5
    I cant work out what it is?
    Thanks

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: random variable

    Did you add the include() line?

    PHP Code:
    include("oneline_random_quote.php"); 
    Otherwise, the function's not in the scope of the script.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    366

    Re: random variable

    Thanks that worked - was idiot. With that code, can i call on the value of the cookie, sorry never used sessions, is that also a cookie? Or is there no cookie yet in this piece of code?

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: random variable

    Sessions can be implemented either using cookies or appending the session ID to the URL. Cookies are the default method; appending SIDs is messy.

    You do not need to worry about that: it is handled for you when you call session_start(). Do not try to read the session cookie manually. Just use the $_SESSION superglobal array to store any data you like and it will be preserved until the session expires (usually after 15 minutes of inactivity).

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    366

    Re: random variable

    I have used exacrly what you (penegate) said above, and it worked fine in a document of its own - as you typed it, but when i embedded the line where it echos random quote, into my html somewhere (i dont thinkm this is the cause of my problem) I have to warnings at the top of the page -
    Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/virtual-web/*****/public/web/link3.php:2) in /home/virtual-web/*****/public/web/link3.php on line 3

    Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/virtual-web/*****/public/web/link3.php:2) in /home/virtual-web/*****/public/web/link3.php on line 3
    Please can you help me with there meanings.
    Thanks
    Alex

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: random variable

    It means you're calling session_start after output has started. visualAd mentioned this in his reply.

    session_start() sets/reads a cookie by sending a header. PHP buffers headers until output begins, then it flushes the headers; you cannot send any after that point.

    Move the session_start() call to the top of the page, or at least, before anything is sent. Make sure that the file begins with <?php. If there is any whitespace before that, it will cause output to begin.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    366

    Re: random variable

    Yeah that makes sense thanks guys, your being very helpful.
    But I have the line that says where to place the value of the cookie, in a table, miles down the html webpage
    Code:
    <td>
    <?php echo($cookievalue);?></td>
    But it still insists to put it at the top of the page, and not in the table?????
    THanks
    Alex

  11. #11
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: random variable

    Where you output values is immaterial. The problem is that your session_start() call comes after some output has already been sent. The warning says line 3 - look at that line.

    For example, this will work:
    PHP Code:
    <?php
      session_start
    ()
    ?>

    <!-- ... -->

    <?php echo $_SESSION['varname'?>
    This will not:
    PHP Code:
    <html>
      <?php session_start() ?>

      <!-- ... -->

      <?php echo $_SESSION['varname'?>
    </html>
    If all else fails, post the source code from the start of the file down to your session_start() call.
    Last edited by penagate; Mar 25th, 2007 at 05:33 AM.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    366

    Re: random variable

    cool, yeah as i understood it didnt work. So here is the sourcecode of the entire page
    Code:
    <?php 
    session_start(); 
    include("oneline_random_quote.php");
    
    if(! isset($_SESSION['quote'])) { 
        $_SESSION['quote'] = randomQuote("quotes.txt"); 
    } 
    
    $cookievalue = $_SESSION['quote']; 
    ?> 
    <html>
    <head>
      <title>template31</title>
      <meta content="Evrsoft First Page" name="GENERATOR">
      <link href="style.css" type="text/css" rel="stylesheet">
    </head>
    
    <body text="#FFFFFF" bottommargin="0" leftmargin="0" background="background.jpg" topmargin="0" rightmargin="0" marginwidth="0" marginheiht="0">
      <table height="22" cellspacing="0" cellpadding="0" width="100%" border="0">
        <tbody>
          <tr valign="top">
            <td width="100%" background="top_bg1.jpg"><img height="22" alt="" src="top_bg1.jpg" width="21" border="0"></td>
    
            <td width="319"><img height="22" alt="" src="top_right1.jpg" width="319" border="0"></td>
          </tr>
        </tbody>
      </table>
    
      <table height="184" cellspacing="0" cellpadding="0" width="100%" border="0">
        <tbody>
          <tr valign="top">
            <td width="100%" background="top_bg2.jpg"></td>
    
            <td width="184"><img height="184" alt="" src="top_right2.jpg" width="184" border="0"></td>
          </tr>
        </tbody>
      </table>
    
      <table height="100%" cellspacing="0" cellpadding="0" width="100%" border="0">
        <tbody>
          <tr valign="top">
            <td width="5">&nbsp;</td>
    
            <td width="160">
              <!--=======copy and paste everything below to the commend tag that says end to make an extra menu.. for optimal effect paste the code after the first BR tag after 'end' comment-->
    
              <table bordercolor="#54B71F" cellspacing="0" cellpadding="2" width="160" bgcolor="#397D18" border="1">
                <tbody>
                  <tr valign="top">
                    <td bordercolor="#397D18" width="160"><!--=== menu header below ======-->Menu <!--============================--></td>
                  </tr>
                </tbody>
              </table>
    
              <table cellspacing="0" cellpadding="2" width="160" bgcolor="#1A3D09" border="0">
                <tbody>
                  <tr valign="top">
                    <td width="160"><!--==== menu links go below ===================================--><a href="-">-</a><br>
                    <a href="-">-</a><br>
                    <a href="-">-</a><br>
                    <a href="">Your link</a><br>
                    <a href="">Your link</a><br>
                    <a href="">Your link</a><br>
                    <br>
                    <br>
                    <br>
                    <br>
                    <!--=============================================================--></td>
                  </tr>
                </tbody>
              </table><!--====================== end =======================================--><br>
            </td>
    
            <td width="10">&nbsp;&nbsp;&nbsp;&nbsp;</td>
    
            <td width="100%">
              <table cellspacing="0" cellpadding="0" width="100%" border="0">
                <tbody>
                  <tr valign="top">
                    <td width="100%">
                      <!--=========page content goes below================================================-->
    
                      <center>
                        <h3>&nbsp;</h3>
    
                        <table bordercolordark="#666666" bgcolor="#669933" bordercolorlight="#333333" border="4">
                          <caption align="top">
                            
                          </caption>
    
                          <tbody>
                            <tr>
                              <td><b>-</b></td>
    
                              <td><b>-</b></td>
    
                              <td><b>-</b></td>
    
                              <td><b>-</b></td>
    
                              <td><b>-</b></td>
                            </tr>
    
                            <tr>
                              <td>-</td>
    
                              <td>
    <?php echo($cookievalue);?></td>
    
                              <td>
                                <center>
                                  -
                                </center>
                              </td>
    
                              <td>
                                <center>
                                 -
                                </center>
                              </td>
    
                              <td>
                                <center>
                                  -
                                </center>
                              </td>
                            </tr>
    
                            <tr>
                              <td>due</td>
    
                              <td>
                                <center>
                                  -
                                </center>
                              </td>
    
                              <td>
                                <center>
                                  -
                                </center>
                              </td>
    
                              <td>
                                <center>
                                  -
                                </center>
                              </td>
    
                              <td>
                                <center>
                                  -
                                </center>
                              </td>
                            </tr>
    
                            <tr>
                              <td>due</td>
    
                              <td>
                                <center>
                                  -
                                </center>
                              </td>
    
                              <td>
                                <center>
                                  -
                                </center>
                              </td>
    
                              <td>
                                <center>
                                  -
                                </center>
                              </td>
    
                              <td>
                                <center>
                                  -
                                </center>
                              </td>
                            </tr>
                          </tbody>
                        </table>
    
                        <center>
                          
    
                          <p></p>
                        </center><br>
                        <br>
                        <br>
                        <br>
                        <br>
                        <br>
                        <br>
                        <br>
                        <br>
                        <br>
                        <br>
                        <br>
                        <br>
                        <br>
                        <br>
                        <br>
                        <br>
                        <br>
    
                        <center>
                          This site is &#169; Copyright Stock prediction 1997-2007, All Rights Reserved.
                        </center><!--======================================================================================--><!-- do not remove -->
                        <!-- end link -->
                      </center>
                    </td>
                  </tr>
                </tbody>
              </table>
            </td>
    
            <td width="5">&nbsp;</td>
    
            <td width="77" background="rightbg.jpg" height="100%"><img height="22" alt="" src="rightbg.jpg" width="77" border="0"></td>
          </tr>
        </tbody>
      </table>
    </body>
    </html>
    And the link to the page as it looks now is
    http://www.ksd-design.co.uk/web/test.php
    The quote2 bit which is at the top of the page, should be in the table, where there is no dash (or the word 'due') you can see there is a gap in the table.
    Thanks
    Alex

  13. #13
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: random variable

    Better still, upload the file. And why can you not move the echo statement to the cell in the table that you want it? You only need session_start() at the beginning of the script.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  14. #14
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: random variable

    It appears as if the random_quote.php is also generating output. Try putting the session_start() cool above the include statement, or check the include file and ensure this does not produce any output.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    366

    Re: random variable

    Hi Guys, I have been reading around and as far as i understand sessions are litterally the length of time the user is on the webpage, I need my cookie to last 6 weeks, so actually how i started is better.

    This is what i am trying to do.

    Webpage checks if there is a cookie allready.
    - If there is it displays the value of the cookie in the table.
    - If there isnt it runs the random quote and puts that in the table, and creates a cookie with the value of the random quote.

    Every 7 days, checks if there is a cookie, if there isnt, it displays a certain line of code across the whole screen, rather than the table or anything,
    If there is a cookie checks the value of the cookie
    - For one value (there are only two because only two random quotes) deletes that cookie, and loops back, so runs through the random quote thingy and saves the value as the value of a new cookie,
    - For the other value displays some text.

    I want to loop this for 6 weeks, How do i do this, I tried with sessions but got very confused and then realised sessions werent what i needed.

    Thanks and sorry guys, I learnt from it anyway.....

  16. #16
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: random variable

    You can set the expiry time of the session using:
    PHP Code:
    ini_set('session.cookie_lifetime'time() + (60*60*24*7*6));
    session_start(); 
    You can of course use cookies too. Personally, I wouldn't store a whole quote in a cookie as it is sent back and forth each time a request is made. Just make sure you call setcookie before any output.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    366

    Re: random variable

    Ok, that sounds good, but i still cant get the value of the session to go into the table. The code is in the table -
    Code:
    <td>
    <?php echo($cookievalue);?></td>
    But it still doesnt go there check http://www.ksd-design.co.uk/web/link3.php

    Please can someone help me?

  18. #18
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: random variable

    I see "quote1" at the top left and "169.45" in R2C3. Is that what's meant to show up?

    By the way, you really shouldn't use tables for layout. Use CSS.

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    366

    Re: random variable

    I am a messy programmer, with not nearly enough experience to clean things up, hence no css - how do i use that to do what you are saying.

    the 169.45 is in the right place but is purely html - and typed there. The quote2 should be where there is an obvious gap in the table to the left of 169.45 But it doesnt go there?

  20. #20
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: random variable

    Quote Originally Posted by youngnoviceinneedofh
    I am a messy programmer, with not nearly enough experience to clean things up, hence no css - how do i use that to do what you are saying.
    http://hotdesign.com/seybold/everything.html


    the 169.45 is in the right place but is purely html - and typed there. The quote2 should be where there is an obvious gap in the table to the left of 169.45 But it doesnt go there?
    It sounds like there is some other cause at work here.

    Make a new page with this code:
    PHP Code:
    <?php
      session_start
    ();
      
    header('Content-type: text/plain');
      
    var_dump($_SESSION);
    ?>
    And post what it displays.
    Last edited by penagate; Mar 29th, 2007 at 01:07 AM. Reason: I meant text/plain, not text/html.

  21. #21

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    366

    Re: random variable

    Thanks - i dont know what you expect out of this but it literally returned

    array(0) { }

  22. #22
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: random variable

    I wrote the wrong content type there; I meant text/plain. But, no matter, because there's nothing to show anyway. It looks as though the session has been started/retrieved, but no session variables have been saved.

    Did you navigate to the page with the random quote before viewing that page? Otherwise it might have timed out.

    Sorry, I'm kind of clutching at straws, because I can't see any solid reason why it shouldn't work.

    If all else fails, a simple cookie is fine, as long as the quote's not a passage from War and Peace or something.

  23. #23

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    366

    Re: random variable

    You know more than i do, but the quote will be a sentence. What i need it to do:
    Webpage checks if there is a cookie allready.
    - If there is it displays the value of the cookie in the table.
    - If there isnt it runs the random quote and puts that in the table, and creates a cookie with the value of the random quote.

    Every 7 days, checks if there is a cookie, if there isnt, it displays a certain line of code across the whole screen, rather than the table or anything,
    If there is a cookie checks the value of the cookie
    - For one value (there are only two because only two random quotes) deletes that cookie, and loops back, so runs through the random quote thingy and saves the value as the value of a new cookie,
    - For the other value displays some text.

    I want to loop this for 6 weeks,
    How do i change what i have done, and do you reckon cookies will get it in the table,

    Secondly, thanks very much about CSS. But what i understand is all CSS does is control the attributes and properties of html statements, so you still have to include a table in the html, all the CSS will do is control what colour etc to include when you put a <tr> etc But the table still has to be there as i have put it?

    Thanks
    Alex

  24. #24
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: random variable

    Do it the same way as sessions, but use $_COOKIE['cookiename'] to read values and setcookie() to save them. Like session_start(), all setcookie() calls must precede any output.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width