Ask Question Forum:
Model Library:2025-02-08 Updated:A.I. model is online for auto reply question page
C
O
M
P
U
T
E
R
2
8
Show
#
ASK
RECENT
←
- Underline
- Bold
- Italic
- Indent
- Step
- Bullet
- Quote
- Cut
- Copy
- Paste
- Table
- Spelling
- Find & Replace
- Undo
- Redo
- Link
- Attach
- Clear
- Code
Below area will not be traslated by Google,you can input code or other languages
Hint:If find spelling error, You need to correct it,1 by 1 or ignore it (code area won't be checked).
X-position of the mouse cursor
Y-position of the mouse cursor
Y-position of the mouse cursor
Testcursor
caretPos
Attachment:===
Asked by duncanb7
at 2024-01-08 07:58:29
Point:500 Replies:9 POST_ID:828875USER_ID:11059
Topic:
PHP Scripting Language;Apache Web Server;Linux
I set cookie of "ticker" by setcookie() in php that is really easy as follows code
But the strange thing is that why it will echo out the correct result
of cookie ticker to be 'testing" after press refresh button on browser
I mean it won't echo out the result in first time run the php file after
I clear up all cookie on browser for testing. it always only echo out
the result after first run php file and run it again at second time(like refresh the page)
Is it related to php version isssue or apache linux server? As remember it won't happen in the past and it is as easy as shown at
http://php.net/setcookie
Please advise
Duncan
But the strange thing is that why it will echo out the correct result
of cookie ticker to be 'testing" after press refresh button on browser
I mean it won't echo out the result in first time run the php file after
I clear up all cookie on browser for testing. it always only echo out
the result after first run php file and run it again at second time(like refresh the page)
Is it related to php version isssue or apache linux server? As remember it won't happen in the past and it is as easy as shown at
http://php.net/setcookie
Please advise
Duncan
Author: duncanb7 replied at 2024-01-09 07:01:02
Thanks Ray's rely even the question is closed.
For my memo only:
Finally, I decided to separate the code into two php pages or files.
One php page is resolved all cookie or seesion issue first , and becoz it is
really small file size so it won't create too long http request time.
The second one is main php page for larger file html code and size that is redirected
in the first php page by header(). That I think it will help on double request loading time issue.
Have a nice day
Duncan
first.php (the first php file)
======================
For my memo only:
Finally, I decided to separate the code into two php pages or files.
One php page is resolved all cookie or seesion issue first , and becoz it is
really small file size so it won't create too long http request time.
The second one is main php page for larger file html code and size that is redirected
in the first php page by header(). That I think it will help on double request loading time issue.
Have a nice day
Duncan
first.php (the first php file)
======================
<?php$path=str_ireplace($_SERVER['DOCUMENT_ROOT'], '', dirname($_SERVER['SCRIPT_FILENAME']));setcookie("ticker","testing", time()+3600*8, $path."/", $_SERVER["HTTP_HOST"], 0,1);if (isset($_COOKIE['ticker'])) {header("location:second.php");}else {header("location:first.php");}?> 1:2:3:4:5:6:7:8:9:10:
Expert: Ray Paseur replied at 2024-01-09 05:27:43
Two HTTP requests will always be slower than one, but before you worry about that part, use a timer to determine how much slower. It may not matter.
Here is the code snippet from the last post above, annotated with comments. It does not really make sense as written. If you want to post a question that describes what you're trying to do (in high-level, non-tehcnical terms) we can probably show you the best-of-breed design pattern.
Here is the code snippet from the last post above, annotated with comments. It does not really make sense as written. If you want to post a question that describes what you're trying to do (in high-level, non-tehcnical terms) we can probably show you the best-of-breed design pattern.
<?php// SET A COOKIE, ...// BUT FOR WHAT SUBDOMAINS?// AND WHAT PATH?// HOW LONG SHOULD THE COOKIE LIVE?// SHOULD IT BE VISIBLE TO JAVASCRIPT OR NOT?setcookie("ticker",'testing');// IF THIS COOKIE ALREADY EXISTED BEFORE THIS HTTP REQUESTif (isset($_COOKIE['ticker'])) {/* ticker cookie is set so do other php code here */ }// IF THIS COOKIE DID NOT ALREADY EXIST BEFORE THIS HTTP REQUEST// MISSING STATEMENT TERMINATOR CAUSES A PARSE ERROR; else header("location:main.php"]// THE SCRIPT WILL NEVER GET TO THIS INSTRUCTION BECAUSE IF/ELSE WILL STOP IT?><html><body></body>......code html here</html> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:
Author: duncanb7 replied at 2024-01-08 20:34:49
Thanks for your reply and good articles, if you have time, please give me some advise on my last post. Use session, session_start(); $_SEESION['ticker']="testing" instead of cookie ? if so, but
session expire time is hard to be controlled in php.ini file
Duncan
session expire time is hard to be controlled in php.ini file
Duncan
Author: duncanb7 replied at 2024-01-08 20:33:06
my issue is fixed with the following code by isset() and header() but it need
do two times HTTP request that may be slower in speed felt by page viewer
Any idea is better than this code ?
Duncan
main.php
==============
do two times HTTP request that may be slower in speed felt by page viewer
Any idea is better than this code ?
Duncan
main.php
==============
<?phpsetcookie("ticker",'testing');if (isset($_COOKIE['ticker'])) {/* ticker cookie is set so do other php code here */ } else header("location:main.php"]?><html><body></body>......code html here</html> 1:2:3:4:5:6:7:8:9:10:11:12:
Expert: Ray Paseur replied at 2024-01-08 09:24:04
One other note... The use of @ in a function call is a code smell, and is to be avoided. It does not suppress the error, it only suppresses the error message; in other words, the error still happened and you just don't know about it. This is one of the many stupid and dangerous things about the PHP language. Such a capability should not even exist. Just don't do that.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12293-AntiPHPatterns-and-AntiPHPractices.html
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12293-AntiPHPatterns-and-AntiPHPractices.html
Accepted Solution
Expert: Ray Paseur replied at 2024-01-08 09:20:28
250 points EXCELLENT
So you mean what I experienced is normal and correct that I only echo out
the result of cookie at second time of http request, Right ?
Right. Please read the article and take note of the part about HTTP Headers Must Come First, Period. You cannot use echo before header() unless you have turned on output buffering, with ob_start() or a configuration setting. the result of cookie at second time of http request, Right ?
Author: duncanb7 replied at 2024-01-08 08:24:05
I try to fix the issue by adding header to the same location of the php file of cookie.php
but it will report to warning
Warning: Cannot modify header information - headers already sent by (output started at /mysite/cookie.php:3) in /mysite/test.php on line 4
How to avoid the warning beside using @ on header() ? Why it will give me the warning
, what special reason behind for checking echo statement on header() function ?
it seems the warning is not harmful or risky to my code but I want to know the reason.
I will read your good article definitely later on. But I need to finish my work first
Please advise & thanks
Duncan
cookie.php file
===============
but it will report to warning
Warning: Cannot modify header information - headers already sent by (output started at /mysite/cookie.php:3) in /mysite/test.php on line 4
How to avoid the warning beside using @ on header() ? Why it will give me the warning
, what special reason behind for checking echo statement on header() function ?
it seems the warning is not harmful or risky to my code but I want to know the reason.
I will read your good article definitely later on. But I need to finish my work first
Please advise & thanks
Duncan
cookie.php file
===============
<?phpsetcookie("ticker",'testing');echo $_COOKIE['ticker']."=====<br/>";header("location: http://mysite.com/cookie.php");?> 1:2:3:4:5:
Author: duncanb7 replied at 2024-01-08 08:05:28
So you mean what I experienced is normal and correct that I only echo out
the result of cookie at second time of http request, Right ?
the result of cookie at second time of http request, Right ?
Assisted Solution
Expert: Ray Paseur replied at 2024-01-08 08:03:22
250 points EXCELLENT
Setcookie() does not modify the contents of $_COOKIE which was loaded at the time of the HTTP request. You can modify $_COOKIE in your code, or you can use the cookie in the next HTTP request. You can get a better understanding of the data flow if you read this article for understanding (don't just scan, it's not a simple process).
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/A_11271-Understanding-Client-Server-Protocols-and-Web-Applications.html
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/A_11271-Understanding-Client-Server-Protocols-and-Web-Applications.html