|
-
Mar 19th, 2007, 10:02 AM
#1
Thread Starter
Hyperactive Member
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>
-
Mar 19th, 2007, 10:46 AM
#2
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).
-
Mar 19th, 2007, 01:58 PM
#3
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.
-
Mar 22nd, 2007, 06:30 AM
#4
Thread Starter
Hyperactive Member
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
-
Mar 22nd, 2007, 06:52 AM
#5
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.
-
Mar 24th, 2007, 10:23 AM
#6
Thread Starter
Hyperactive Member
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?
-
Mar 24th, 2007, 10:28 AM
#7
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).
-
Mar 24th, 2007, 10:59 AM
#8
Thread Starter
Hyperactive Member
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
-
Mar 24th, 2007, 11:03 AM
#9
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.
-
Mar 25th, 2007, 05:25 AM
#10
Thread Starter
Hyperactive Member
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
-
Mar 25th, 2007, 05:30 AM
#11
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.
-
Mar 25th, 2007, 05:46 AM
#12
Thread Starter
Hyperactive Member
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"> </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"> </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> </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 © Copyright Stock prediction 1997-2007, All Rights Reserved.
</center><!--======================================================================================--><!-- do not remove -->
<!-- end link -->
</center>
</td>
</tr>
</tbody>
</table>
</td>
<td width="5"> </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
-
Mar 25th, 2007, 06:06 AM
#13
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.
-
Mar 25th, 2007, 06:08 AM
#14
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.
-
Mar 25th, 2007, 09:10 AM
#15
Thread Starter
Hyperactive Member
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.....
-
Mar 25th, 2007, 02:23 PM
#16
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.
-
Mar 27th, 2007, 09:28 AM
#17
Thread Starter
Hyperactive Member
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?
-
Mar 27th, 2007, 09:39 AM
#18
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.
-
Mar 27th, 2007, 09:44 AM
#19
Thread Starter
Hyperactive Member
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?
-
Mar 27th, 2007, 10:27 AM
#20
Re: random variable
 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.
-
Mar 28th, 2007, 10:26 AM
#21
Thread Starter
Hyperactive Member
Re: random variable
Thanks - i dont know what you expect out of this but it literally returned
array(0) { }
-
Mar 29th, 2007, 01:12 AM
#22
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.
-
Mar 29th, 2007, 04:52 AM
#23
Thread Starter
Hyperactive Member
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
-
Mar 29th, 2007, 06:03 AM
#24
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|