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-08-19 02:56:30
Point:500 Replies:19 POST_ID:828751USER_ID:11059
Topic:
PHP Scripting Language;;Linux
From the following link, I am able to access my desktop cookie from my php script
which is running must be at the same path & same domain for cookie
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28215476.html
For example,
the cookie domain:www.mysite.com
the cookie path /wordpress/wp-admin
my php script domain and path : www.mysite.com/wordpress/wp-admin/example.php
The question,
Could I access the cookie at different path within the same domain ?
FOr example, I put example.php at www.mysite.com/temp/php/example.php and
read the cookie
Please advise
Duncan
which is running must be at the same path & same domain for cookie
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28215476.html
For example,
the cookie domain:www.mysite.com
the cookie path /wordpress/wp-admin
my php script domain and path : www.mysite.com/wordpress/wp-admin/example.php
The question,
Could I access the cookie at different path within the same domain ?
FOr example, I put example.php at www.mysite.com/temp/php/example.php and
read the cookie
Please advise
Duncan
Expert: Rob Jurd replied at 2024-08-23 14:56:49
Duncan thanks for the points but you should know how to grade the answers
http://support.experts-exchange.com/customer/portal/articles/481419-what-grade-should-i-award-
http://support.experts-exchange.com/customer/portal/articles/481419-what-grade-should-i-award-
Author: duncanb7 replied at 2024-08-23 04:55:09
Thanks for all of your reply.
Accepted Solution
Expert: Rob Jurd replied at 2024-08-23 00:53:58
167 points GOOD
That's exactly right. If you want a cookie then look at using a session that will do exactly what you have specified. Let me know and I'll explain more but I didn't want to go off topic :-)
Author: duncanb7 replied at 2024-08-23 00:38:09
So you mean
under at mysite.com/wordpress/ , all different directory path that is okay
if under at mysite.com/temp/php that is not okay,
What I want, I want get the cookie at all different directory not just under
/wordpress/ directory , of course it must be under same domain
DUncan
under at mysite.com/wordpress/ , all different directory path that is okay
if under at mysite.com/temp/php that is not okay,
What I want, I want get the cookie at all different directory not just under
/wordpress/ directory , of course it must be under same domain
DUncan
Assisted Solution
Expert: Rob Jurd replied at 2024-08-19 06:54:06
166 points GOOD
because your trying to access it from /temp/php, which isn't in the same directory structure.
try putting your/temp directory under your WordPress site
e.g.
wordpress/wp-admin/temp/php
try putting your/temp directory under your WordPress site
e.g.
wordpress/wp-admin/temp/php
Author: duncanb7 replied at 2024-08-19 06:36:59
for example, I only send
Ray's php script(example-cookie.php) that is working only at
http://www.mysite.com/wordpress/wp-admin/
since the cookie path saved on my broswer is same as wordpress/wp-admin/
But when I put the example-cookie.php at http://www.mysite.com/temp/php
it doesn't work or doesn't show cookie from "wordpress/wp-admin/" path, why ?
Ray's php script(example-cookie.php) that is working only at
http://www.mysite.com/wordpress/wp-admin/
since the cookie path saved on my broswer is same as wordpress/wp-admin/
But when I put the example-cookie.php at http://www.mysite.com/temp/php
it doesn't work or doesn't show cookie from "wordpress/wp-admin/" path, why ?
Expert: Rob Jurd replied at 2024-08-19 06:21:43
Sure I understand, Duncan :-)
so that I understand your problem, is there a WordPress cookie being set and used that you want to be able to access it from another script on the same domain?
if so then it can't be done. the cookie relates to the path it was created in. as your script would be outside of this path the browser doesn't send the cookie data or make that cookie available on your page
so that I understand your problem, is there a WordPress cookie being set and used that you want to be able to access it from another script on the same domain?
if so then it can't be done. the cookie relates to the path it was created in. as your script would be outside of this path the browser doesn't send the cookie data or make that cookie available on your page
Author: duncanb7 replied at 2024-08-19 06:10:19
Dear tagit,
Thanks for your input
Could we focus cookie first ? and I will execise session in next new thread,
Please be reminded
DUncan
Thanks for your input
Could we focus cookie first ? and I will execise session in next new thread,
Please be reminded
DUncan
Expert: Rob Jurd replied at 2024-08-19 05:53:31
The cookie can only be read from the same path or sub-path from where the cookie was created.
What you're after is a session where the data traverses the domain not the path.
/test/path1/index.php:
What you're after is a session where the data traverses the domain not the path.
/test/path1/index.php:
<?phpsession_start();setcookie('testing','show me the cookie');$_SESSION['testing']='show me the session';?><html><head></head><body></body></html> 1:2:3:4:5:6:7:8:9:
/test/index.php
AND
/test/path2/index.php:
<?phpsession_start();?><html><head></head><body><?echo print_r($_COOKIE,true);echo '<br/>';echo print_r($_SESSION,true);?></body></html> 1:2:3:4:5:6:7:8:9:10:11:12:13:
path2 outputs:
Array ( [PHPSESSID] =>" e88dac7f1f14167a09f2663690b12f68 )Array ( [testing] =>" show me the session ) 1:2:
meaning the cookie didn't come across but the session data did
Author: duncanb7 replied at 2024-08-19 05:34:53
same as Ray sent it in this thread, you can try that
Expert: Rob Jurd replied at 2024-08-19 05:33:58
If you are doing any kind of authentication, I would suggest that you also take a look at sessions (http://www.php.net/manual/en/intro.session.php). What are you storing in the cookie? It's easily visible using any of the major browsers developer tools. However with a session that data is stored on the server. The user has no idea what data is stored in a session. Keep in mind that cookies and sessions can be used side by side, it's not necessarily one or the other.
Expert: Rob Jurd replied at 2024-08-19 05:28:02
What code do you have at the moment for setting and reading your cookie? If we can't see that then we don't know where you're going wrong.
Expert: Ray Paseur replied at 2024-08-19 05:28:01
I don't know what to tell you. Since I cannot see your code or data, about all I can do is suggest some tests that might help you reveal what is happening. The default values for setcookie() may not give the results you want.
Author: duncanb7 replied at 2024-08-19 04:46:54
Dear Ray, I am not taking about setcookie at different path,
I am saying reading client cookie at my server at different path for
page authentation checking before accessing into my theme pages
Please advise
Duncan
I am saying reading client cookie at my server at different path for
page authentation checking before accessing into my theme pages
Please advise
Duncan
Author: duncanb7 replied at 2024-08-19 04:35:58
Ray, the script is same as before you sent to me ?
And it just only works at wordpress/wp-admin/ that is the same path of cookie that I can see
it on browser
and my script php is only working when it is running at /wordpress/wp-admin on browser
but not working at /temp/php/
So it seems I have to put the script only at the same path of cookie in order to
get the cookie information.
How to make it work at different path ?
Duncan
And it just only works at wordpress/wp-admin/ that is the same path of cookie that I can see
it on browser
and my script php is only working when it is running at /wordpress/wp-admin on browser
but not working at /temp/php/
So it seems I have to put the script only at the same path of cookie in order to
get the cookie information.
How to make it work at different path ?
Duncan
Expert: Rob Jurd replied at 2024-08-19 04:29:49
great example @Ray
Assisted Solution
Expert: Ray Paseur replied at 2024-08-19 03:53:08
167 points GOOD
Please install this script and run it. Try running it from different paths. Look closely at lines 44 and 45. Try setting the cookie for different paths. See what happens. Take notes. Your question seems to arise from either not reading the documentation about cookies or not understanding the documentation about cookies, and when I find myself in that circumstance, I find that running discreet test cases is the best way to prove to myself how something works!
<?php // RAY_cookie_example.phperror_reporting(E_ALL);// RECEIVE FORM INPUT AND SET A COOKIE WITH THE NAME, VALUE AND LIFE FROM THE FORM// MAN PAGE: http://php.net/manual/en/function.setcookie.php// TO SEE COOKIES IN FIREFOX, FOLLOW SOMETHING LIKE TOOLS => OPTIONS => PRIVACY => SHOW COOKIES (OR "REMOVE INDIVIDUAL")// REQUIRED AT PHP 5+date_default_timezone_set('America/New_York');// IF THE FORM HAS BEEN POSTEDif (!empty($_POST)){ // TIDY UP THE POST INPUT - CLEAN AND NOT MORE THAN 16 BYTES $name = substr(clean_string($_POST["name"]),0,16); $data = substr(clean_string($_POST["data"]),0,16); $life = clean_number($_POST["life"]); // BE SURE WE HAVE USEFUL INFORMATION if ( ($name == '') || ($data == '') ) die("MISSING INPUT: PLEASE <a href="{$_SERVER['PHP_SELF']}">TRY AGAIN</a>"); // CHOOSE THE COOKIE NAME, VALUE, AND LIFE IN SECONDS $cookie_name = $name; $cookie_value = $data; $cookie_life = $life; // CONFIGURE THE COOKIE LIFE if ($life == 0) { // USE THIS TO MAKE COOKIE EXPIRE AT END OF BROWSER LIFE $cookie_expires = 0; } else { // USE THIS TO MAKE A PERSISTENT COOKIE $cookie_expires = time() + $cookie_life; } // MAKE THE COOKIE AVAILABLE TO ALL DIRECTORY PATHS $cookie_path = DIRECTORY_SEPARATOR; // MAKE THE COOKIE AVAILABLE TO ALL SUBDOMAINS - DOMAIN NAME STARTS WITH DOT AND OMITS WWW (OR OTHER SUBDOMAINS). $x = explode('.', strtolower($_SERVER["HTTP_HOST"])); $y = count($x); if ($y == 1) // MAYBE 'localhost'? { $cookie_domain = $x[0]; } else // SOMETHING LIKE 'www2.atf70.whitehouse.gov'? { // USE THE LAST TWO POSITIONS TO MAKE THE HOST DOMAIN $cookie_domain = '.' . $x[$y-2] . '.' . $x[$y-1]; } // MAKE THE COOKIE AVAILABLE TO HTTP, NOT JUST HTTPS $cookie_secure = FALSE; // HIDE COOKIE FROM JAVASCRIPT (PHP 5.2+) $cookie_http = TRUE; // SET THE COOKIE if ( setcookie ( $cookie_name , $cookie_value , $cookie_expires , $cookie_path , $cookie_domain , $cookie_secure , $cookie_http ) ) { echo PHP_EOL . "<br/>SUCCESS! THE COOKIE HAS BEEN SET AND WILL BE AVAILABLE TO THE NEXT PAGE LOAD"; } else { echo PHP_EOL . "<br/>FAILURE! THE COOKIE WAS NOT SET AS EXPECTED"; } // AT THIS POINT, THE COOKIE HAS BEEN SET, BUT IT IS NOT AVAILABLE IN THIS SCRIPT // THE COOKIE WILL NOT BE RETURNED FROM THE CLIENT TO THE SERVER UNTIL THE NEXT HTTP REQUEST // THIS IS BECAUSE THE BROWSER SENDS THE COOKIE TO OUR SCRIPT BEFORE OUR SCRIPT STARTS RUNNING echo '<pre>'; echo PHP_EOL . '$_COOKIE CONTAINS '; var_dump($_COOKIE); echo PHP_EOL . '$_POST CONTAINS '; var_dump($_POST); echo PHP_EOL . 'THE COOKIE HAS BEEN SET WITH THESE VALUES: '; echo PHP_EOL . 'COOKIE NAME: ' . $cookie_name; echo PHP_EOL . 'COOKIE VALUE: ' . $cookie_value; echo PHP_EOL . 'COOKIE EXPIRES: ' . number_format($cookie_expires) . " == " . date('r', $cookie_expires); echo PHP_EOL . 'COOKIE PATH: ' . $cookie_path; echo PHP_EOL . 'COOKIE DOMAIN: ' . $cookie_domain; echo PHP_EOL . 'COOKIE SECURE: '; var_dump($cookie_secure); echo 'COOKIE HTTP: '; var_dump($cookie_http); echo "</pre>";}// END OF SETTING THE COOKIE - CREATE THE FORM IN HEREDOC NOTATION$self = $_SERVER["PHP_SELF"];$form = <<<FORM<pre><form method="post">COOKIE NAME: <input name="name" /> STRINGCOOKIE DATA: <input name="data" /> STRINGCOOKIE LIFE: <input name="life" /> INTEGER SECONDS<input type="submit" /><b>TO SEE THE EXISTING COOKIES <a href="$self">CLICK HERE</a></b></form></pre>FORM;echo $form;// SHOW THE COOKIE ARRAY, IF ANYecho '<pre>$_COOKIE CONTAINS '; var_dump($_COOKIE); echo "</pre>";// UNRELATED FUNCTIONS TO TIDY UP FORM INPUTfunction clean_string($string){ return trim(preg_replace('/[^A-Z0-9_]/i', NULL, $string));}function clean_number($string){ return trim(preg_replace('/[^0-9]/i', 0, $string));}// ACTIVATE THIS TO SHOW THE SCRIPT CODE// die( highlight_file(__FILE__, TRUE) ); 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:26:27:28:29:30:31:32:33:34:35:36:37:38:39:40:41:42:43:44:45:46:47:48:49:50:51:52:53:54:55:56:57:58:59:60:61:62:63:64:65:66:67:68:69:70:71:72:73:74:75:76:77:78:79:80:81:82:83:84:85:86:87:88:89:90:91:92:93:94:95:96:97:98:99:100:101:102:103:104:105:106:107:108:109:110:111:112:113:114:115:116:117:118:119:120:121:122:123:124:125:126:127:128:129:130:131:132:133:134:135:136:137:138:139:140:141:
To fully understand the effect of setting a cookie, you will need a script that can visualize the cookie after it has been returned to the server. This will do that part for you. You can install it in several different directories to see exactly what the client sends back the server.
Expert: Rob Jurd replied at 2024-08-19 03:49:02
http://www.w3schools.com/php/php_cookies.asp
you may need to post your JavaScript cookie to your php script to set it via php to make it available to all your scripts
you may need to post your JavaScript cookie to your php script to set it via php to make it available to all your scripts
Expert: Rob Jurd replied at 2024-08-19 03:24:42
if you set the cookie in php on the same domain you'll be able to access it in any php script on that domain.