Password Protecting Xml With Php
<h3 class="heading-h6"><a name="THDLToolboxHomegtDevelopersZonegtUsingPasswordProtectiongtPasswordProtectingXML" class="anchorpoint"></a><a href="/tools/wiki/Home.html">THDL Toolbox</a> > <a href="/tools/wiki/Developers%27%20Zone.html">Developers' Zone</a> > <a href="/tools/wiki/Using%20Password%20Protection.html">Using Password Protection</a> > Password Protecting XML</h3><p class="paragraph">
</p><h3 class="heading-h1"><a name="PasswordProtectingXMLwithPHP" class="anchorpoint"></a>Password Protecting XML with PHP</h3><p class="paragraph"><strong class="bold">Contributor(s)</strong>: Than Garson</p><p class="paragraph"><strong class="bold">Note:</strong> this procedure may be outdated and this documentation may need to be rewritten.</p><p class="paragraph">To do this, the URL call has to be to a PHP file that pipes the results from the SaxonServlet into the browser. In the case of JIATS, there is an index.php file at the root level of the jiats folder. All sub folders then need to have a redirect to the root folder so people can't view the contents on those folders. The index.php file should have at the top code that checks to see if a cookie for the password has been set. If the password is not set, then it redirects to a login.php script. There are 3 main components to this part of the PHP:</p><ol><li><strong class="bold">$_SERVER["QUERY_STRING"]</strong>: This PHP global variable contains the query string for the URL call. This is everything after the question mark "?". "QUERY_STRING" is the constant key for this.</li>
<li><strong class="bold">$HTTP_COOKIE_VARS["pwd"]</strong>: This PHP global hash-array contains the values for all the cookies in the document. It returns the value of the cookie whose name is given in the quotes. In this case, it is "pwd". Another alternative way of retrieving a cookie is the global variable $_COOKIE['pwd']. This is used in the login script below to get the "query" cookie.</li>
<li><strong class="bold">setcookie(cookie_name,cookie_value,duration)</strong>: This command sets a cookie for the page. It has to be called before any <html> header information is sent. The duration is the amount of time in seconds that the cookie will remain active, after which it will expire.</li></ol><p class="paragraph">The code in the index.php for the journal is:
</p><div class="code"><pre><?php
$query = $_SERVER['QUERY_STRING']; // Get the query string
$pwd = $HTTP_COOKIE_VARS[<span class="java-quote">"pwd"</span>]; // get the <span class="java-quote">"pwd"</span> cookie value
<span class="java-keyword">if</span>($pwd != <span class="java-quote">"let-me-in"</span>) { // <span class="java-keyword">if</span> the <span class="java-quote">"pwd"</span> value is not equal to the password, <span class="java-keyword">for</span> instance, <span class="java-quote">"let-me-in"</span>, then …
setcookie(<span class="java-quote">"query"</span>,$query,time()+3600); // set a cookie with the value of the query string to be used
// once one gets in then print out an html file that redirects one to the login page
?>
<html>
<head>
<meta http-equiv=<span class="java-quote">"Content-Type"</span> content=<span class="java-quote">"text/html; charset=utf-8"</span>/>
<!-- The redirect to the login page below //-->
<meta http-equiv=<span class="java-quote">"refresh"</span> content=<span class="java-quote">"0;url=/collections/journal/jiats/login.php"</span>>
<title>Journal of the International Association of Tibetan Studies</title>
<link rel=<span class="java-quote">"stylesheet"</span> type=<span class="java-quote">"text/css"</span> href=<span class="java-quote">"/style/thdl-styles.css"</span> />
</head>
<body><p>Need to login …</p>
</body>
</html>
<?php
exit;
}
?>
<!-- the <span class="java-keyword">rest</span> of the code (not given here) is the code <span class="java-keyword">for</span> display the journal.
If the password is correctly set, then the script skips the <span class="java-keyword">if</span> statement above and executes <span class="java-keyword">this</span> code --></pre></div><p class="paragraph">
The login page is also a PHP file that prints out a simple login form and calls itself using POST. If the correct password is given, "let-me-in", then it sets that as the cookie "pwd" and redirects to the original index.php page, which now lets the user into the journal. The code for this login page is:</p><div class="code"><pre><?php
$pwd = $_POST['pwd'];
<span class="java-keyword">if</span>($pwd == ' ') { $pwd = $HTTP_COOKIE_VARS[<span class="java-quote">"pwd"</span>];}
<span class="java-keyword">if</span>($pwd!='let-me-in') {
echo <span class="java-quote">"<html><head><title>JIATS Not Yet Released <span class="java-keyword">for</span> Public Viewing!</title>\n"</span>;
echo <span class="java-quote">"<script type=\"</span>text/javascript\<span class="java-quote">" src=\"</span>/scripts/thdl_scripts.js\<span class="java-quote">"></script>\n"</span>;
echo <span class="java-quote">"<link rel=\"</span>stylesheet\<span class="java-quote">" type=\"</span>text/css\<span class="java-quote">" href=\"</span>/style/thdl-styles.css\<span class="java-quote">"/>\n"</span>;
echo <span class="java-quote">"</head><body><script type=\"</span>text/javascript\<span class="java-quote">" src=\"</span>/scripts/banner.js\<span class="java-quote">"></script>\n"</span>;
echo <span class="java-quote">"<div id=\"</span>main\<span class="java-quote">" class=\"</span>text-heavy\<span class="java-quote">"><h2>JIATS Validation</h2>\n"</span>;
echo <span class="java-quote">"<p style=\"</span>width: 500px; text-align: justify;\<span class="java-quote">">The <i>Journal of the International Association of Tibetan Studies</i> has not yet been officially released. "</span>;
echo <span class="java-quote">"To view it, you must be an approved member of the board with the appropriate password. Otherwise, the first issue will be released shortly. "</span>;
echo <span class="java-quote">"Thank you <span class="java-keyword">for</span> your patience! </p>\n"</span>;
<span class="java-keyword">if</span>(strlen($pwd) > 0) {echo <span class="java-quote">"<p>You entered "</span> . $pwd . <span class="java-quote">"!</p>"</span>;}
echo <span class="java-quote">"<form method=\"</span>post\<span class="java-quote">" action=\"</span><img src="/" alt="external link: " title="external link"/><span class="nobr"><a href="http://orion.lib.virginia.edu/thdl/collections/journal/jiats/login.php\" target="rwikiexternal">http://orion.lib.virginia.edu/thdl/collections/journal/jiats/login.php\</a></span><span class="java-quote">">\n"</span>;
echo <span class="java-quote">"<table width=\"</span>50%\<span class="java-quote">"><tr><td>Enter password: <input type=\"</span>password\<span class="java-quote">" name=\"</span>pwd\<span class="java-quote">" /></td></tr>\n"</span>;
echo <span class="java-quote">"<tr><td><input type=\"</span>submit\<span class="java-quote">"/></td></tr></table></form></div></body></html>\n"</span>;
exit;
} <span class="java-keyword">else</span> {
setcookie(<span class="java-quote">"pwd"</span>,$pwd,time()+43200);
setcookie(<span class="java-quote">"query"</span>,<span class="java-quote">""</span>);
?>
<html>
<head>
<meta http-equiv=<span class="java-quote">"Content-Type"</span> content=<span class="java-quote">"text/html; charset=utf-8"</span>/>
<?php
$query = $_COOKIE['query];
<span class="java-keyword">if</span> ($query == <span class="java-quote">""</span>) {
echo <span class="java-quote">"<meta http-equiv=\"</span>refresh\<span class="java-quote">" content=\"</span>0;url=index.php\<span class="java-quote">">"</span>;
} <span class="java-keyword">else</span> {
echo <span class="java-quote">"<meta http-equiv=\"</span>refresh\<span class="java-quote">" content=\"</span>0;url=index.php?$query\<span class="java-quote">">"</span>;
}
?>
<title>Journal of the International Association of Tibetan Studies</title>
<link rel=<span class="java-quote">"stylesheet"</span> type=<span class="java-quote">"text/css"</span> href=<span class="java-quote">"/style/thdl-styles.css"</span> />
</head>
<body></body>
</html>
<?php
} ?></pre></div><p class="paragraph">
</p><h3 class="heading-h6"><a name="ProvidedforunrestrictedusebytheTibetanandHimalayanDigitalLibrary" class="anchorpoint"></a><em class="italic">Provided for unrestricted use by the Tibetan and Himalayan Digital Library</em></h3>