[[Fixed]]
Was accessing the file directly in my browser, instead of through the localhost web server
--
I'm fairly new to PHP and trying to get the following to work.
This code is in the middle of a HTML page.
I've initialized and set the variable right above the if statement
I'm checking if the variable is '1', when I've set it to '3'.
My problem is, that it is still running the html code in the IF statement when it shouldn't be
Any idea's?
Thanks in advance
<?php
$ad='3';
if($ad=='1')
{?>
<p>Broken</p>
<?php
}
?>
I found the problem.
You have created a HTML file and using php inside that which is totally wrong .
To get php code
working you need to create a php file and write php code
create a php file by saving a file as filename.php and copy those contents there, which will work fine
Also make sure that you have php installed on you system.If not download and install wamp server
You have to check the test field inside $_SESSION array! You are checking the whole array... :)
<?php
session_start();
$_SESSION['test']='2';
if($_SESSION['test']=='1')
{?>
<!--HTML Code-->
<?php
}
?>
You have also to check if the $_SESSION var is set:
session_start();
if(isset($_SESSION['test']))
{
if($_SESSION['test']=='1')
{
//do stuff HTML code
}
elseif($_SESSION['test']=='2')
{
//do other stuff HTML code
}
}
Related
I have a PHP file (myFile.php) that parses XML, and assigns the data I need to a variable ($myVar). I need to echo this variable on an HTML page. I’ve tried adding <?php include ‘/myFile.php’; echo $myVar; ?> to the HTML file to no avail, however if I call the script itself (http://localhost/myFile.php) and add echo $myVar; to the end of the file the data is displayed as expected.
Am I missing something simple? I’m running PHP7 on a Linux Apache server - is there a setting somewhere that I need to change? I’ve used this syntax in the past without issue.
Thank you for the second set of eyes!
Try the following:
<?php
include $_SERVER['DOCUMENT_ROOT'] . '/myFile.php';
echo $myVar;
?>
can you please have a look at these codes and let me know why mysql query doesn't work?
in a php file I add a check box to my page with following code
echo "<input type=checkbox name=box[] onClick=\"deleteLink('$ClickedWord','$rLinks');\"'>";
then in another php file "deleteLink" function exists and its code is:
function deleteLink($clword,$DltLinks)
{
<?php
session_start(); // start up your PHP session!
$u= $_SESSION['Unit'];
$f= $_SESSION['file'];
mysql_query("DELETE FROM links WHERE ((Unit_Code='$u') && (File_Name='$f')&& (Word='$clword')&& (Link_Add='$DltLinks'))") or die(mysql_error());
?>
}
I am sure that this file executes but doesn't delete the record. I did some tests to find the problem but no result!!!
I think you're missing something here. I'm pretty sure you can't directly call a php function with an onClick handler. You'd have to write a javascript function that creates and AJAX request to pass the values to the php page with the function.
I think the reason that you think it's being called is that you're actually calling it as you're creating the page.
I have a PHP file consisting of the following structure:
<html>... headers, scripts & styling
... some html here
<?php
if($_GET['v'] == 1)
{
?>
... html code here ...
<?php
}
else
{
?>
... html code here ...
<?php
}
?>
</html>
Sometimes the file just loads half, for example if v=1 what would load onto the screen (if I check with View Source also) is something like this: (relative to what I exampled above)
<html>... headers, scripts & styling
... some html here
... html cod
As you can see, the code just cuts off randomly. The is nothing obvious casing this such as a loop or anything. It happens in the middle of HTML code and not inside the <?php ?> tags.
It looks as if the server just decides to stop communicating right there-and-then for no reason. It also happens at a different and random place each time and sometimes loads perfectly fine.
It also only happens on my shared hosting account and not on my localhost.
Is there anything simples that might be causing this?
Did anyone experience this before?
Your code produces a warning (apparently silent) and fails here:
if($_GET['v'] == 1)
if no v parameter was given in the query string.
Do it like this:
if(isset($_GET['v']) && $_GET['v'] == 1)
If you're running an old version of PHP you'll have to make two separate if statements for each of the two conditions.
Make sure you have display_errors turned on.
ini_set('display_errors',1);
Just to make sure there's nothing going horribly wrong.
If I were to use a PHP script for dynamic CSS (as in, not only writing to the CSS stylesheet, but called by the link line in place of a stylesheet), would $_REQUEST or any similar functions work? I'm having issues and it seems like that's the closest reason why my script keeps malfunctioning - it can do an SQL query perfectly fine when the query is whole and assigned to a variable, but when I attempt to call in a script that uses $_REQUEST and builds a query that way, it fails (despite working perfectly when called in other non-CSS-related scripts).
EDIT: Ok, I've just figured out the main issue. It seems that $_GET works for the link tag, i.e., "href='image.php?page=index'".
However, I want to be able to use $_REQUEST to get something from the URL, like how it is used in non-CSS-related scripts. Is there any way for me to do this?
Yes, all those superglobals are available no matter what you use your script for. The interpreter has no knowledge of what type of data the script is going to output. Your error must be somewhere else in the code. Are you outputting the correct header to tell the browser that it is css?
header('Content-Type: text/css');
<link href="css.php?id=1&value=2" rel="text/css">
first make sure that you specify the header in your css.php file
header('Content-Type: text/css');
Now you can have access to the query string:
$id = $_GET['id'];
$value = $_GET['value'];
Example:
body {
<?php
if ($id == 1) {
echo "background-color: red";
}else {
echo "background-color: yellow";
}
?>
this should perfectly work
With my data files I use with sites I usually include some PHP code in them to prevent them being directly accessed, such as below..
<?php
if (defined("VALID")) {
?>
html code here
<?php
} else {
die('Restricted Access.');
}
?>
Now this works fine when I do a simple include..... however I am using one of these files to do some replacements in & hence need to make use of file_get_contents(); however when using this, not only do I get the HTML code, I obviously also get the PHP code returned with it..... this ends up going in the source, which I do NOT want.
Is there any way around this? Perhaps stripping the PHP code? Any better ways/suggestions?
If you want to make replacements on an output of a script try using output buffering.
Instead of file_get_contents('your-php-script.php') do this:
ob_start();
include('your-php-script.php');
$contents = ob_get_clean();
// do your replacements on a $contents
echo preg_replace("~<\?php(.*?)\?>~", "", $contents);
This should work to erase the PHP code in the file.
Why dont you use a hashed string in a session cookie to check it? I think its the best solution. So add to the cookie a hashed value, then check for that value on the file you need to check if its valid and voila!
Hope it helps!