I recently created a new login-register system for myself but i'm having issues with htmls indenting when echoed from php, Now that i've really looked at it, i realized that echoing a entire page isnt the smartest idea, unless it is?
<?php
if (isset($_SESSION['sessionId'])) {
echo('This is the new content after login.');
} else{
echo 'This is the login page';
}
?>
above is my index.php the first echo would echo the entire page of content the user would see after they logged in.
Second is the login form.
what would be the easiest way to go about this?
You can do something like this
<?php
if (isset($_SESSION['sessionId'])) {
readfile("/path/to/html/file/new_content.html");
} else{
readfile("/path/to/html/file/login.html");
}
?>
You can try echoing your HTML output like so
<?php
if (isset($_SESSION['sessionId'])) {
echo '<br><p>';
echo'This is the new content after login.';
echo '</p>';
} else{
echo '<br><p>';
echo '  This is the login page';
echo '</p>';
}
?>
You'll see the above code in view source if you use it
Output:
<br>
<p>
  This is the content after login
</p>
second output
<br>
<p>
  This is the login page
</p>
P.S. BTW why are you using brackets for one echo and none for the other
???
Related
Im new to Php. I kinda need code to open Links in browser when script is loaded.
heres my code below.
<?php
$links = array_open("https://stackoverflow.com/",
"https://outlook.office.com/",
"https://www.protectedtext.com/",
"https://www.adobe.com/",
"https://www.linkedin.com");
echo $links[array_rand($links)];
?>
Hi change array_open to array
<?php
$links = array("https://stackoverflow.com/", "https://outlook.office.com/", "https://www.protectedtext.com/", "https://www.adobe.com/", "https://www.linkedin.com");
echo $links[array_rand($links)];
?>
And if you want to send the browser to the link you would send a location header like so...
<?php
$links = array("https://stackoverflow.com/", "https://outlook.office.com/", "https://www.protectedtext.com/", "https://www.adobe.com/", "https://www.linkedin.com");
header('Location: ' . $links[array_rand($links)]);
exit;
?>
How do I replace echo with proper html codes? Since there will be hundreds of html codes, using echo doesn't make sense.
<?php
if ($this->item->catid == 9) {
echo "yes, it exists";
}
else {
echo "no, it don't exist";
}
?>
I'm new to PHP.
Do you mean, how can one cleanly output many lines of HTML conditionally without having to echo each individual line?
If that's the case, something like this would do the trick:
<?php
if ($this->item->catid == 9) {
?>
<!--- put all the raw output you like here --->
<?php
} else {
?>
<!--- put all the raw output you like here --->
<?php
}
?>
The "raw output" would be anything sent to the browser. HTML, JavaScript, etc. Even though it's outside of the server-side PHP tags, as far as the PHP processor is concerned it's still between the { ... } brackets. So it's only output as part of that conditional block.
There is two way to do it :
1) As suggested by David, by closing your php tags to write raw HTML.
<?php
if ($this->item->catid == 9) {
?>
// HTML goes here
<?php
}else{
?>
// HTML goes here
<?php
}
?>
But if you're planning to write a lot of text it might be a be hard to read your code in the end so you can use the following.
<?php
$htmlOutput = '';
if ($this->item->catid == 9) {
$htmlOutput .= "yes, it exists";
} else {
$htmlOutput .= "no, it doesn't exist";
}
?>
You create a variable that will contain all your HTML by appending a part of it everytime you need to so in the end you'll only need to print a single variable.
on my website I want to show a Logout button when a user is logged in and a logout button when the user is not logged in.
I wrote this code:
if(isset($_SESSION["username"])){ echo "<li class='xy'><button class='x' onclick='location.href = 'index.php';'>Logout</button></li>"; } else { my login button... }
The buttons do appear but unfortunately the are not clickable.
How can I solve this problem?
You are not properly escaping quotations
Replace:
onclick='location.href = 'index.php';'
with:
onclick='location.href = \'index.php\';'
You need to escape the single quotes as they are mixing up with each other.
Corrected Code:
<?php
if (isset($_SESSION["username"])){
echo "<li class='xy'><button class='x' onclick='location.href = \'index.php\';'>Logout</button></li>";
}
else {
//my login button...
}
Another solution can be not to write HTML in PHP.
PHP and HTML can be embedded into each other without any restriction.
You are embedding HTML into PHP. Embed PHP in HTML:
<?php
if (isset($_SESSION["username"])){
?>
<li class='xy'><button class='x' onclick="location.href = 'index.php'">Logout</button></li>
<?php
}
else {
//my login button...
}
If you just want to send the user to an URL target (index.php in your example) use a link, instead of re-inventing a link and simulating it using JS. Style the link to look like a button. (There are plenty CSS examples on the web on how to do that.)
<?php
if( isset( $_SESSION["username"] ) ) {
echo '<li class="xy"><a class="button x" href="index.php">Logout</a></li>';
}
else {
// my login button...
}
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.
Hi I'm trying to get a piece of html to only show on the main page which is http://www.domain.com/ ... I wrote the code below but it doesn't work the HTML is showing regardless of the page, am I missing something
<?php
$hweb .= 'http://' .$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
if ($hweb == 'http://www.domain.com/'):
?>
<div style="margin:0 auto;">
<div style="float:left">
<?php endif; ?>
First of all - please change
$hweb .= 'http://' .$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
into
$hweb = 'http://' .$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$hweb may be initialized somewhere before.
Second:
As long as you request 'http://www.domain.com/somename.php' your if condition will never get executed. REQUEST_URI will always hold '/somename.php' except you use some url rewriting.
Third:
Make sure all calls go to 'http://www.domain.com' and not to 'http://domain.com'. Subdomain configurtaions sometimes are very complicated.
At the risk of getting it wrong again..
Why not initialize a variable in the main file before including the header
<?php
$mainfile = true;
?>
then in the header
<?php
if ($mainfile===true)
....
This way the main file can be called anything and be placed anywhere.
Solution 1:
If the above code is written inside 'http://www.domain.com/index.php' file then it may work fine.
Solution 2:
else make sure that $hewb is set with null value earlier b4 this code so that ".=" would not add extra value b4 'http...'.
Now
$hweb = '';
echo $hweb .= 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
That is because the the HTML is inline in the php file but outside of the PHP tags. You can simply echo the HTML inside the if.
if ($hweb == 'http://www.domain.com/')
{
echo '<div style="margin:0 auto;">';
echo '<div style="float:left">';
}
or if you have lots of HTML you could do it like this
<?php
ob_start();
?>
<html>
<body>
<p>This HTML only be echoed </p>
</body>
</html>
<?php
$hweb .= 'http://' .$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
if ($hweb == 'http://www.domain.com/'):
{
ob_end_flush();
}
else
{
ob_end_clean(); // Probably not needed
}
?>