Results 1 to 7 of 7

Thread: Cookie code, whats wrong?

  1. #1

    Thread Starter
    Frenzied Member wiz126's Avatar
    Join Date
    Jul 2005
    Location
    Mars,Milky Way... Chit Chat Posts: 5,733
    Posts
    1,080

    Question Cookie code, whats wrong?

    whats wrong with my cookie code? I am pretty sure my code SHOULD work 100%, yet it doesn't.....
    Code:
    <?php
    if ( $_GET["RSN"] == "" || $_GET["RSN"] == "null")
    {
    	$username = $_COOKIE["TipMeName"];
    	//echo '<script type="text/javascript">alert("Getting An Allready Set Cookie: '.$_COOKIE["TipMeName"].'")</script>';
    } else { 
    	setcookie("TipMeName", $_GET["RSN"] , time()+5184000 );
    	header( 'refresh: 0; url=/1/' );
    	//echo'<script type="text/javascript">alert("Setting A New Cookie: '.$_COOKIE["TipMeName"].' - '.$_GET["RSN"].'")</script>';
    }
    
    
    if (isset($_COOKIE["TipMeName"]))
    {
      echo
    		'
    		<fieldset>
    		<table border="0" width="100%">
    			<tr>
    				<td>
    				<p align="center"><font size="2">Your RS Name Is:"' .$username. '"</font><br>
    				<form action="#" method="get">
    				RSN: 
    				<input type="text" name="RSN" size="20">
    				<input type="submit" value="Change It">
    				</form>
    				</p>
    				</td>
    			</tr>
    		</table>
    		</fieldset>
    		';
    } else {
      echo
    				'
    				<fieldset>
    		<table border="0" width="100%">
    			<tr>
    				<td>
    				<p align="center"><font size="2">---text goes here the i removed-- 
    				tools.</font></td>
    			</tr>
    			<tr>
    				<td>
    				<p align="center"><b><font size="2" color="#FF0000">No RS Username is set!</font></b><br>
    				<form action="#" method="get">
    				RSN: 
    				<input type="text" name="RSN" size="20">
    				<input type="submit" value="Save Name">
    				</form>
    				</p>
    				</td>
    			</tr>
    		</table>
    		</fieldset>
    		';	
    }
    ?>
    Last edited by wiz126; Jan 28th, 2007 at 12:10 PM.
    1) If your post has been adequately answered please click in your post on "Mark Thread Resolved".
    2) If someone has been useful to you please show your respect by rating their posts.
    3) Please use [highlight="VB"] 'your code goes in here [/highlight] tags when posting code.
    4) Before posting your question, make sure you checked this links:
    MICROSOFT MSDN -- VB FORUMS SEARCH

    5)Support Classic VB - A PETITION TO MICROSOFT

    ___________________________________________________________________________________
    THINGS TO KNOW ABOUT VB: || VB Examples/Demos
    What are Classes?
    || -
    Where to place a sub/function?(global) || Webbrowser control

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Cookie code, whats wrong?

    Cookies you just set aren't immediately available in the _COOKIES superglobal, only after the next browser request.

    Or is your problem different? If so, tell us what it is.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Frenzied Member wiz126's Avatar
    Join Date
    Jul 2005
    Location
    Mars,Milky Way... Chit Chat Posts: 5,733
    Posts
    1,080

    Re: Cookie code, whats wrong?

    Quote Originally Posted by CornedBee
    Cookies you just set aren't immediately available in the _COOKIES superglobal, only after the next browser request.

    Or is your problem different? If so, tell us what it is.
    that is correct, and thats why I placed this code:
    Code:
    		header( 'refresh: 0; url=/1/' );
    I just realized that my code works perfectly fine when its on a page of its own. but once I copy and past it into my main page. it stops working....
    1) If your post has been adequately answered please click in your post on "Mark Thread Resolved".
    2) If someone has been useful to you please show your respect by rating their posts.
    3) Please use [highlight="VB"] 'your code goes in here [/highlight] tags when posting code.
    4) Before posting your question, make sure you checked this links:
    MICROSOFT MSDN -- VB FORUMS SEARCH

    5)Support Classic VB - A PETITION TO MICROSOFT

    ___________________________________________________________________________________
    THINGS TO KNOW ABOUT VB: || VB Examples/Demos
    What are Classes?
    || -
    Where to place a sub/function?(global) || Webbrowser control

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

    Re: Cookie code, whats wrong?

    did you send any output before executing that code?

    once you send output to the browser, cookie and header functions cannot be executed.
    Last edited by kows; Jan 28th, 2007 at 01:24 PM.

  5. #5

    Thread Starter
    Frenzied Member wiz126's Avatar
    Join Date
    Jul 2005
    Location
    Mars,Milky Way... Chit Chat Posts: 5,733
    Posts
    1,080

    Re: Cookie code, whats wrong?

    Quote Originally Posted by kows
    did you send any output before executing that code?

    once you send output to the browser, cookie and header functions cannot be executed.
    Can you explain a bit more?
    1) If your post has been adequately answered please click in your post on "Mark Thread Resolved".
    2) If someone has been useful to you please show your respect by rating their posts.
    3) Please use [highlight="VB"] 'your code goes in here [/highlight] tags when posting code.
    4) Before posting your question, make sure you checked this links:
    MICROSOFT MSDN -- VB FORUMS SEARCH

    5)Support Classic VB - A PETITION TO MICROSOFT

    ___________________________________________________________________________________
    THINGS TO KNOW ABOUT VB: || VB Examples/Demos
    What are Classes?
    || -
    Where to place a sub/function?(global) || Webbrowser control

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Cookie code, whats wrong?

    setcookie() adds a "Set-Cookie" header to the set of headers sent to the browser. Headers must always be sent before the page content; that's a HTTP requirement.

    When PHP first encounters normal content, unless content buffering is on, it sends the headers and starts the content output. Thus, after any normal content was encountered, the setcookie() function has no effect anymore.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Frenzied Member wiz126's Avatar
    Join Date
    Jul 2005
    Location
    Mars,Milky Way... Chit Chat Posts: 5,733
    Posts
    1,080

    Re: Cookie code, whats wrong?

    I see, Thank you
    1) If your post has been adequately answered please click in your post on "Mark Thread Resolved".
    2) If someone has been useful to you please show your respect by rating their posts.
    3) Please use [highlight="VB"] 'your code goes in here [/highlight] tags when posting code.
    4) Before posting your question, make sure you checked this links:
    MICROSOFT MSDN -- VB FORUMS SEARCH

    5)Support Classic VB - A PETITION TO MICROSOFT

    ___________________________________________________________________________________
    THINGS TO KNOW ABOUT VB: || VB Examples/Demos
    What are Classes?
    || -
    Where to place a sub/function?(global) || Webbrowser control

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