I am trying to display the username and other session values stored in session at login.php. I post the form in index.php using ajax post which is in script.js. The session is stored but I can retrieve the value only after I refresh the page.
Can anyone please help me sort this issue.
My index.php file
<div id="profile">
<?php session_start();
if(isset($_SESSION['user_name']) && $_SESSION['user_name'] != ''){
echo "Hello ".$_SESSION['user_name'];
?>
<a href='logout.php'>Logout</a>
<?php } ?>
</div>
My script.js file
success: function(html){
if(html=='true'){
$("#login_form").fadeOut("normal");
$("#profile").html("<a href='logout.php' class='red' id='logout'>Logout</a>");
}
My login.php
echo 'true';
$_SESSION['user_name']='username';
At that time I need two view pages in one page. So that in the session i do something like this:
<?php
session_start();
if(isset($_SESSION['login_id']) && !empty($_SESSION['login_id'])){
?>
///My HTML CODE///
<?
}
else {
?>
//Diffetent View HeRe//
<?
}
require_once('../libraries/config/configPDO.php');
?>
But now I just need to make it become one page and redirect it if they not logged in.
<?php
session_start();
if(isset($_SESSION['login_id']) && !empty($_SESSION['login_id'])){
?>
///My HTML CODE///
<?
}
else {header("Location: login.php");
?>
<?
}
require_once('../libraries/config/configPDO.php');
?>
But as you see, I put the header redirect at the bottom of the page.
How to make it simple, so that I set header location in the top of the page.
Try with -
session_start();
if(empty($_SESSION['login_id'])){
header("Location: login.php");
exit;
} else {
//HTML
}
try this,
<?php
ob_start();
session_start();
if(isset($_SESSION['login_id']) && !empty($_SESSION['login_id'])){
?>
///My HTML CODE///
<?
require_once('../libraries/config/configPDO.php');
}
else {
header("Location: login.php");
}
?>
reverse the if to
if(!isset($_SESSION['login_id']) || empty($_SESSION['login_id'])){
and swap the cases
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>
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.
How can I add script inside a php code? suppose i want to give an alert for a button click.. how can i do that??
You can just echo all the HTML as normal:
<?php
echo '<input type="button" onclick="alert(\'Clicky!\')"/>';
?>
<?php
echo"<script language='javascript'>
</script>
";
?>
You mean JavaScript? Just output it like anything else in the page:
<script type="text/javascript">
<?php echo "alert('message');"; ?>
</script>
If want PHP to generate a custom message for the alert dialog, then basically you want to write your JavaScript as usual in the HTML, but insert PHP echo statements in the middle of your JavaScript where you want the messages, like:
<script type="text/javascript">
alert('<?php echo $custom_message; ?>');
</script>
Or you could even do something like this:
<script type="text/javascript">
var alertMsg = '<?php echo $custom_message; ?>';
alert(alertMsg);
</script>
Basically, think about where in your JavaScript you want PHP to generate dynamic output and just put an echo statement there.
To avoid escaping lot of characters:
echo <<<MYSCRIPT
... script here...
MYSCRIPT;
or just turn off php parsing for a while:
?>
...your script here
<?php
You could use PHP's file_get_contents();
<?php
$script = file_get_contents('javascriptFile.js');
echo "<script>".$script."</script>";
?>
For more information on the function:
http://php.net/manual/en/function.file-get-contents.php
You mean you want to show a javascript alert when a button is clicked on a PHP generated page?
echo('<button type="button" onclick="alert(\'Alrt Text!\');">My Button</button>');
Would do that
You can insert script to HTML like in any other (non-PHP) page, PHP processes it like any other code:
<button id="butt">
→ Click ME! ←
</button>
<script>
document.getElementById("butt").onclick = function () {
alert("Message");
}
</script>
You can use onSOMETHING attributes:
<button onclick="alert('Message')">Button</button>
To generate message in PHP, use json_encode function (it can convert to JavaScript everything that can be expressed in JSON — arrays, objects, strings, …):
<?php $message = "Your message variable"; ?>
<button onclick="alert(<?=htmlspecialchars(json_encode($message), ENT_QUOTES)?>)">Click me!</button>
If you generate code for <script> tags, do NOT use htmlspecialchars or similar function:
<?php $var = "Test string"; ?>
<button id="butt">Button</button>
<script>
document.getElementById("butt").onclick = function () {
alert(<?=json_encode($var)?>);
}
</script>
You can generate whole JavaScript files, not only JavaScript embedded into HTML. You still have to name them with .php extension (like script.php). Just send the correct header.
script.php – The JavaScript file
<?php header("Content-Type: application/javascript"); /* This meant the file can be used in script tag */ ?>
<?php $var = "Message"; ?>
document.getElementById("butt").onclick = function () {
alert(<?=json_encode($var)?>);
}
index.html – Example page that uses script.php
<!doctype html>
<html lang=en>
<head>
<meta charset="utf-8">
<title>Page title</title>
</head>
<body>
<button id="butt">
BUTTON
</button>
<script src="script.js"></script>
</body>
</html>
Exactly the same way you add HTML tags. Echo it
One way to avoid accidentally including the same script twice is to implement a script management module in your templating system. The typical way to include a script is to use the SCRIPT tag in your HTML page.
<script type="text/javascript" src="menu_1.0.17.js"></script>
An alternative in PHP would be to create a function called insertScript.
<?php insertScript("menu.js") ?>
To add javascript inside a PHP code you can do this
<?php
echo "<script>alert('message');</script>";
?>
Better this
<?php
echo "<script src='myScript.js'></script>";
?>
And this for WordPress function.php file
<?php
$script= get_template_directory_uri() . '/myScript.js';
echo "<script src=".$script."></script>";
?>
In your php file you can do something like this :
<?
//Your php code
?>
<script type="text/javascript">
//Your javascript code
</script>
<?php //Your php code