Including php variable in header() - php

I'm still learning php and I still haven't figured out when to use ' or ". I'm guessing thats the problem with this code. It redirects me to the right page but the $loc variable isn't carried over.
<?php header("Location: roomdata.php?loc=$loc"); ?>
on the page that has the header() commaned I also have an include command...
<?php include 'include/globalscripts.php'; ?>
and in the globalscripts.php is...
<?php if( isset($_GET['loc']))?>
<?php $loc = $_GET["loc"];?>

I would personally use:
<?php
header('Location: roomdata.php?loc='.$loc);
?>

<?php if( isset($_GET['loc']))?>
<?php $loc = $_GET["loc"];?>
TRY
<?php
if( isset($_GET['loc'])){
$loc = $_GET["loc"];
}
?>

Your code should work, are you sure $loc is defined at this point?
Regarding ' and ":
$value = "derp";
echo "the value is:\t$value";
//output: the value is: derp
echo 'the value is:\t$value';
//output: the value is:\t$value

Related

How to pass value to file being called in include_once

When i tried to pass a value $abc to the IncludedFile.php, it doesnt go through. Neither does the $session work. Is there a way to pass a value to the IncludedFile.php? My ultimate aim is to get the calculation processed at the IncludedFile.php and then echo it on the Main.php
Main.php
<?php $abc = 'abc';
include_once ('IncludedFile.php');
echo $answer; ?>
IncludedFile.php
<?php if($abc=='abc') {$answer = 5;} ?>

PHP script tags

Why does this if statement have each of its conditionals wrapped in PHP tags?
<?php if(!is_null($sel_subject)) { //subject selected? ?>
<h2><?php echo $sel_subject["menu_name"]; ?></h2>
<?php } elseif (!is_null($sel_page)) { //page selected? ?>
<h2><?php echo $sel_page["menu_name"]; ?></h2>
<?php } else { // nothing selected ?>
<h2>Select a subject or a page to edit</h2>
<?php } ?>
Because there is html used. Jumping between PHP and HTML is called escaping.
But I recommend you not to use PHP and HTML like this. May have a look to some template-systems e.g. Smarty or Frameworks with build-in template-systems like e.g. Symfony using twig.
Sometimes its ok if you have a file with much HTML and need to pass a PHP variable.
Sample
<?php $title="sample"; ?>
<html>
<title><?php echo $title; ?></title>
<body>
</body>
</html>
This is not much html but a sample how it could look like.
That sample you provided us should more look like....
<?php
if(!is_null($sel_subject))
{ //subject selected?
$content = $sel_subject["menu_name"];
}
else if (!is_null($sel_page))
{ //page selected?
$content = $sel_page["menu_name"];
}
else
{ // nothing selected
$content = "Select a subject or a page to edit";
}
echo "<h2>{$content}</h2>";
?>
You could echo each line of course. I prefer to store this in a variable so I can easy prevent the output by editing one line in the end and not each line where I have added a echo.
According to some comments i did a approvement to the source :)
Because the <h2> tags are not PHP and will display an error if the PHP Tags are removed.
This code will display one line of text wrapped in <h2> tags.
This is called escaping.
Because you cannot just type html between your php tags.
However, I would rather use the following syntax because it is easier to read. But that depends on the programmers opinion.
<?php
if(!is_null($sel_subject))
{ //subject selected?
echo "<h2>" . $sel_subject["menu_name"] . "</h2>";
}
elseif (!is_null($sel_page))
{ //page selected?
ehco "<h2>" . $sel_page["menu_name"] . "</h2>";
}
else
{ // nothing selected
echo "<h2>Select a subject or a page to edit</h2>";
}
Because inside the if-statement there is an HTML code, which you can put it by closing PHP tags and open it again like this:
<?php if(/*condition*/){ ?> <html></html> <?php } ?>
or:
<?php if(/*condition*/){ echo '<html></html>' ; }
That is because in this snippet we see html and php code. The code <?php changes from html-mode to php-mode and the code ?> changes back to html-mode.
There are several possibilites to rewrite this code to make it more readable. I'd suggest the following:
<?php
//subject selected?
if (!is_null($sel_subject)) {
echo "<h2>" . $sel_subject["menu_name"] . "</h2>";
//page selected?
} elseif (!is_null($sel_page)) {
echo "<h2>" . $sel_page["menu_name"] . "</h2>";
// nothing selected
} else {
echo "<h2>Select a subject or a page to edit</h2>";
}
?>
using the echo-command to output html, you don't need to change from php-mode to html-mode and you can reduce the php-tag down to only one.

Rendering a Global or a Var in a Php echo

I found a good script anti spam robots. To implement it in my dynamic pages I have to paste this code:
<?php echo hide_email('test#test.com'); ?>
Since my pages are dynamic, my emails are stored in Globals
%%GLOBAL_Email%%
<?php echo hide_email('%%GLOBAL_Email%%'); ?> <--- The email is not rendered
Even if I transform the Global in a var it does not work, I tryed the following:
<?php echo hide_email(' . $email . '); ?>
<?php echo hide_email('" . $email . "'); ?>
Answer:
<?php echo hide_email($email); ?>
The question was simple, the answer too.
Here is the answer:
<?php echo hide_email($email); ?>

How to add link within if-statement?

I have some problems to put a URL-function within an if-statement properly. The following method works fine outside of my if-statement and the newly created link refers to this URL: "h**p://www.blog.com/?location=Bern&date=1"
<?php $rasp = $_GET["location"]; ?>
<a href="<?php echo preg_replace("/&date=(0|1|2|3)/", "", $_SERVER[\REQUEST_URI\])."&date=0"; ?> " >Heutel</a>
If-Statement works also just fine when I put simply an URL (e.g. https://www.google.com/) in between the quotation marks, then if I put the URL-function above in there, the newly created link refers to this: "h**p://www.blog.com/%3C?php%20echo%20preg_replace%28". Actually it should refer to the URL above "h**p://www.blog.com/?location=Bern&date=1"
URL-Function within if-statement:
<?php $url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (false !== strpos($url,'date=1')) { echo
'<?php $rasp = $_GET["location"]; ?>
<a href="<?php echo preg_replace("/&date=(0|1|2|3)/", "", $_SERVER[\REQUEST_URI\])."&date=0"; ?> " >Heutel</a> ' ;
} else {
echo 'No cars.';
} ?>
Any ideas?
Solution thanks to IMSop:
<?php
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (false !== strpos($url,'date=1')) {
?>
<a href="<?php echo preg_replace("/&date=(0|1|2|3)/", "", $_SERVER['REQUEST_URI'])."&date=0"; ?> " >Heutel</a>
<?php
}
else {
echo 'No cars.';
}
This sequence makes no sense:
echo '<a href="<?php echo ...
A quoted string and direct output outside the <?php ... ?> markers are completely different things. The ' starts a string, which will continue until you put another '; the <?php would start a block of PHP code if you weren't in one, but you already are - if you weren't the echo wouldn't mean anything.
To join multiple strings together, you can use the . ("concatenation") operator:
echo '>Heutel';
Alternatively, drop out of PHP mode like you were before, but with the if in place:
<?php
// In PHP mode...
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (false !== strpos($url,'date=1')) {
// Now leaving PHP mode, but still inside the if condition...
?>
<a href="<?php echo preg_replace("/&date=(0|1|2|3)/", "", $_SERVER['REQUEST_URI'])."&date=0"; ?> " >Heutel</a>
<?php
// Re-enter PHP mode to close off the if statement
}
There's even an alternative syntax for control syntax which some people prefer to use in cases like this:
<?php
if ($some_condition) :
?>
your output here
<?php
endif;
?>
which is exactly the same as
<?php
if ($some_condition) {
?>
your output here
<?php
}
?>

php how to scan full page for all POST variables and then echo at top of page?

I can't echo my variable above my CMS include code.. but if I echo the variable after, then it recognizes the $url variable.
Here is some code:
<?php
// here is my CMS inlcude code
$template = 'news_script';
$number = '';
$category = '';
include $cutepath.'/show_news.php';
?>
If I echo $url above the include code, it returns nothing. But below, it obviously recognizes it.
Is there a php function that scans the whole page and retrieves all the POST variables so you can use $url at the top of the php page with a header('Location:'. $url); script??
Obviously $url is being defined in your show_news.php script. PHP executes a script line-by-line, and will not magically "reach back" to set a variables value in an earlier line.
Another (ugly) way would be:
<?php
// here is my CMS inlcude code
$template = 'news_script';
$number = '';
$category = '';
ob_start();
include $cutepath.'/show_news.php';
$buffered_data=ob_get_contents();
ob_end_clean();
echo $url; // here you could place your: header('Location:'. $url);
echo $buffered_data;
?>

Categories