Wampserver doesn't handle PHP scripts (sometimes)
Posted by: Polda18 (---.sh.cvut.cz)
Date: February 06, 2017 02:06PM

Hello. I have some troubles with Wampserver. I was able to setup two virtual hosts (and restore them using vhosts config and Windows hosts file). One virtual host runs developing instance of WordPress (I'm developing own themes). That one works fine. But the other host holds few small scripts, that I can never get to work sad smiley I don't know why sad smiley

This is one of the two scripts I have written:
<?php

/////////////////////////////////////////
// Náhodné citáty
/////////////////////////////////////////

$citaty = [ // ["citát", "autor"]
["Kdo víno má a&nbsp;nepije,<br />kdo hrozny má a&nbsp;nejí je,<br />kdo ženu má a&nbsp;nelíbá, kdo zábavě se&nbsp;vyhýbá,<br />na&nbsp;toho vemte bič a&nbsp;hůl,<br />to není člověk, to je vůl!", "Jan Werich"],
["Až se budeš v životě cítit nejhůř, otoč se ke slunci, a všechny stíny padnou za tebe.", "John Lennon"],
["Jen dvě věci na světě jsou nekonečné: vesmír a lidská hloupost.", "Albert Einstein"],
["Skutečný život začíná až ve 40 letech. Do té doby jsme si dělali jen průzkum.", "Carl Gustav Jung"],
["Když už člověk jednou je, tak má koukat aby byl. A když kouká, aby byl a je, tak má být to, co je a nemá být to, co není, jak tomu v mnoha případech je.", "Jan Werich"],
["Kdo se umí smát sám sobě, má právo smát se všemu ostatnímu, co mu k smíchu připadá.", "Jan Werich"],
["Látce rozumíte bezpečně teprve tehdy, když jste schopný ji vysvětlit vlastní babičce.", "Albert Einstein"]
];

$nahodny_citat = array_rand($citaty);

?><!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<title>Generátor citátů</title>
<style type="text/css">
blockquote {
background: #f9f9f9;
border-left: 10px solid #ccc;
margin: 1.5em 10px;
padding: 0.5em 10px;
quotes: "\201C""\201D""\2018""\2019";
}
blockquote:before {
color: #ccc;
content: open-quote;
font-size: 4em;
line-height: 0.1em;
margin-right: 0.25em;
vertical-align: -0.4em;
}
blockquote p {
display: inline;
}
.author {
margin-left: 12em;
}
</style>
</head>
<body>
<blockquote>
<p><?php echo $nahodny_citat[0]; ?></p>
<span class="author">&emdash;&nbsp;<?php echo $nahodny_citat[1]; ?></span>
</blockquote>
</body>
</html>

The supposed result is returning a random cite from the given array. But this is all I get:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<title>Generátor citátů</title>
<style type="text/css">
blockquote {
background: #f9f9f9;
border-left: 10px solid #ccc;
margin: 1.5em 10px;
padding: 0.5em 10px;
quotes: "\201C""\201D""\2018""\2019";
}
blockquote:before {
color: #ccc;
content: open-quote;
font-size: 4em;
line-height: 0.1em;
margin-right: 0.25em;
vertical-align: -0.4em;
}
blockquote p {
display: inline;
}
.author {
margin-left: 12em;
}
</style>
</head>
<body>
<blockquote>
<p></p>
<span class="author">&emdash;&nbsp;</span>
</blockquote>
</body>
</html>

So clearly, the PHP scripts are pretty much ignored and striped out from the HTML code, without executing anything. What the hell does that mean? How can I get WordPress (also built on PHP platform) working, but not small scripts that clearly follow the syntax? As far as I know, the PHP service should be running regardless of vhost and all vhosts should have the same settings for interpreter... Or am I missing something?

Options: ReplyQuote
Re: Wampserver doesn't handle PHP scripts (sometimes)
Posted by: Otomatic (Moderator)
Date: February 06, 2017 02:31PM

Hi,

To be interpreted by PHP, files MUST have the extension .php.
A file with the .html extension will never be interpreted by PHP, even if it contains PHP code.

---------------------------------------------------------------
Documentation Apache - Documentation PHP - Documentation MySQL - Wampserver install files & addons

Options: ReplyQuote
Re: Wampserver doesn't handle PHP scripts (sometimes)
Posted by: Polda18 (---.sh.cvut.cz)
Date: February 06, 2017 03:59PM

I can asure you both of the files have .php extension. And it's really strange that WordPress works without any flaws, but handmade scripts, made using few tutorials and PHP manual, don't work at all. WordPress is hosted in another virtual host, than my handmade scripts, but both maintained by Wampserver.

In addition: it's not my first touch with PHP, I just returned to it and tried out another free virtual server, as EasyPHP became premium only, free download without licence doesn'T allow for additions like virtual hosts, which is something I need for WordPress testing, so I bet to Wampserver rather. Since then, I'd also like to build my own small PHP gadgets, so that's why I'm asking...



Edited 1 time(s). Last edit at 02/06/2017 04:02PM by Polda18.

Options: ReplyQuote
Re: Wampserver doesn't handle PHP scripts (sometimes)
Posted by: Otomatic (Moderator)
Date: February 06, 2017 04:32PM

Hi,

Before accusing Wampserver or I do not know what trick, check your code and mainly what are supposed to do the functions you use.

ALWAYS read the official documentation, eg for the function array_rand() which you make a misuse assuming that the return is an array.
« Picks one or more random entries out of an array, and returns the key (or keys) of the random entries. »

You must use :
<p><?php echo $citaty[$nahodny_citat][0]; ?></p>
<span class="author">&mdash;&nbsp;<?php echo $citaty[$nahodny_citat][1]; ?></span>
 

This forum is not intended to debug your scripts. It is up to you to verify that the codes and functions you use are in the rules and to do this, it is better than going to read the documentation whose links are given in the signatures of RiggsFolly and myself as well as in the menus of Wampmanager. RTFM !!!

---------------------------------------------------------------
Documentation Apache - Documentation PHP - Documentation MySQL - Wampserver install files & addons

Options: ReplyQuote
Re: Wampserver doesn't handle PHP scripts (sometimes)
Posted by: Polda18 (---.sh.cvut.cz)
Date: February 06, 2017 05:56PM

You're right, it was my code that caused troubles. I have debug warninga disabled, so I was unable to confirm it was my script. So I must have something screwed up in my second script, password generator.

Options: ReplyQuote
Re: Wampserver doesn't handle PHP scripts (sometimes)
Posted by: Otomatic (Moderator)
Date: February 07, 2017 12:58PM

Hi,

> I have debug warninga disabled, so I was unable to confirm it was my script...
Even without a debugger, it is relatively simple to perform some basic checks to verify the content of the array and the result of array_rand:
error_log("citaty=".print_r($citaty,true));

$nahodny_citat = array_rand($citaty);

error_log("rand=".print_r($nahodny_citat,true));
And, after running, you'll see the contents of php_error.log.

---------------------------------------------------------------
Documentation Apache - Documentation PHP - Documentation MySQL - Wampserver install files & addons

Options: ReplyQuote


Sorry, only registered users may post in this forum.