None of the other methods work so could you please help? Here is my code:
<?php
include("db.php");
If(isset($_SESSION['name']))
{
$main = "<h2>hey</h2>";
}
else
{
$main = "<h2>welcome</h2>"
}
?>
<html>
<body>
<?php echo $main;?>
</body>
</html>
//db.php consists of
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("notes");
echo mysql_error();
session_start();
?>
Whenever I run the code it always shows: welcome. No matter if I'm logged in or logged out of my website.
Your html is looks good but
It seems that you did not set session_start() at the top of your php file or you did not set session variable.
try like this:
<?php
session_start();
if(isset($_SESSION['name']))
{
$main = "<h2>hey</h2>";
}
else
{
$main = "<h2>welcome</h2>"
}
?>
<html>
<body>
<?php echo $main;?>
</body>
</html>
page show2.php:
<?php
session_start();
$_SESSION["abc"]=2;
print $_SESSION["abc"];
include 'import/function.php';
?>
<html>
.............
</html>
page show1.php:
<?php
session_start();
$_SESSION["abc"]=1;
print $_SESSION["abc"];
include 'import/function.php';
?>
<html>
.............
</html>
I've bind an ajax_call to a btn in html that print $_SESSION["abc"]
session_start();
include 'import/function.php';
if(isset($_POST["data"]))
{
print $_SESSION["abc"];
}
When I go in show1.php the page print 1 and ajax_call print 1.
When I go in show2.php the page print 2 but ajax_call print 1! why?
I am new to php. Here is a code to check if the user is logged in using session and then allowing the user.
VALIDATION
<?php
session_start();
$uname = $_POST['uname'];
$pass = $_POST['pass'];
if($uname == "admin" && $pass == "admin")
{
$_SESSION['uname'] = $uname;
$_SESSION['auth'] = 1;
echo "Welcome Mr ".$uname.". You are now logged in ";
echo "<br>";
echo "<a href='TakeMeHome.html'>Click here to access the application </a>";
}
else
{
echo "Invalid username or password";
}
?>
Page
<?php
session_start();
if($_SESSION['auth'] != 1)
{
echo "You are not logged in! ";
echo "<a href = \"TakeMeHome.html\">";
echo "Access Application";
echo "</a>";
exit();
}
?>
<html>
You are now logged in
</html>
But the link tag is displaying
"; echo "Access Application"; echo ""; exit(); } ?>
along with the html data. No verification is done. I know there are many better ways to validate user is logged in or not. But i am learning sessions and hence i am using sessions.
Can you please tell me where i am going wrong?
regards.
use single quote in your echo codes like this:
<html>
<head>
</head>
<body>
<?php
echo "<a href='pageToGoTo.html' title='Page to go to' class='whatEver'>Anchor text</a>";
?>
</body>
</html>
What is told already, html should be put in the body...
I have no idea why #Andy has suggested you put your PHP in your head tags - it isn't javascript. You have 2 ways you can format your PHP and HTML, the first is to put all your PHP above your opening html tag, like so
<?php
session_start();
if($_SESSION['auth'] != 1) {
$message = 'You are not logged in! Access Application';
} else {
$message = 'You are logged in!';
}
?>
<html>
<head>
</head>
<body>
<?php echo $message; ?>
</body>
</html>
Or, place it in the body of your page, like so:
<?php
session_start();
?>
<html>
<head>
</head>
<body>
<?php
if($_SESSION['auth'] != 1) {
echo 'You are not logged in! Access Application';
} else {
echo 'You are logged in!';
}
?>
</body>
</html>
If you are still not getting the desired results then use var_dump($_SESSION); to print out your session array and make sure it holds the correct information.
I am not very good at php but I want to detect if a user is in the home of the website such as;
<?php
if (current path = http://website.com/ || website.com)
{?>
<div>We are in home</div>
<?php } else { ?>
<div>do something</div>
<?php } ?>
Someone help me please thanks
$pageName = $_SERVER['PHP_SELF'];
if($pageName == 'http://website.com'){
echo '<div>We are in home</div>';
} else { do something };
My code works to a point. What I want is that when this if statement is false, the <div> doesn't show
<?php
$query3 = mysql_query($query3);
$numrows = mysql_num_rows($query3);
if ($numrows > 0) {
$fvisit = mysql_fetch_array($result3);
}
else {
}
?>
You can use css or js for hiding a div. In else statement you can write it as:
else{
?>
<style type="text/css">#divId{
display:none;
}</style>
<?php
}
Or in jQuery
else{
?>
<script type="text/javascript">$('#divId').hide()</script>
<?php
}
Or in javascript
else{
?>
<script type="text/javascript">document.getElementById('divId').style.display = 'none';</script>
<?php
}
This does not need jquery, you could set a variable inside the if and use it in html or pass it thru your template system if any
<?php
$showDivFlag=false
$query3 = mysql_query($query3);
$numrows = mysql_num_rows($query3);
if ($numrows > 0){
$fvisit = mysql_fetch_array($result3);
$showDivFlag=true;
}else {
}
?>
later in html
<div id="results" <?php if ($showDivFlag===false){?>style="display:none"<?php } ?>>
A fresh look at this(possibly)
in your php:
else{
$hidemydiv = "hide";
}
And then later in your html code:
<div class='<?php echo $hidemydiv ?>' > maybe show or hide this</div>
in this way your php remains quite clean
Use show/hide method as below
$("div").show();//To Show
$("div").hide();//To Hide
<?php
$divStyle=''; // show div
// add condition
if($variable == '1'){
$divStyle='style="display:none;"'; //hide div
}
print'<div '.$divStyle.'>Div to hide</div>';
?>
Probably the easiest to hide a div and show a div in PHP based on a variables and the operator.
<?php
$query3 = mysql_query($query3);
$numrows = mysql_num_rows($query3);
?>
<html>
<?php if($numrows > null){ ?>
no meow :-(
<?php } ?>
<?php if($numrows < null){ ?>
lots of meow
<?php } ?>
</html>
Here is my original code before adding your requirements:
<?php
$address = 'meow';
?>
<?php if($address == null){ ?>
no meow :-(
<?php } ?>
<?php if($address != null){ ?>
lots of meow
<?php } ?>
from php you can invoke jquery like this but my 2nd method is much cleaner and better for php
if($switchView) :?>
<script>$('.container').hide();</script>
<script>$('.confirm').show();</script>
<?php endif;
another way is to initiate your class and dynamically invoke the condition like this
$registerForm ='block';
then in your html use this
<div class="col" style="display: <?= $registerForm?>">
now you can play with the view with if and else easily without having a messed code example
if($condition) registerForm = 'none';
Make sure you use 'block' to show and 'none' to hide. This is far the easiest way with php
If you wrap the whole block within the <?php ?> tags in the initial 'if' statement and don't specify an 'else', this condition is not satisfied, returns false so therefore won't display the <div>