header:location not working - php

I am some reason having trouble with this, my header redirect does not seem to work.
<?php
session_start();
if(!session_is_registered(myusername)){
header('Location:home.php');
exit;
}
?>
<html>
<body>
Login Successful
</body>
</html>

Try this :
header("Location: home.php"); // there is a space

Hmm, try
if(!isset($_SESSION['myusername'])){
instead of
if(!session_is_registered(myusername)){

Related

PHP :Anyone know Session handling like Facebook page does?

Problem :
User is already logged in
so when I type
local.test_php.com/form/
It takes me to Login page. local.test_php.com/form/index.php
But i want it to take it to my home page instead.(http://local.test_php.com/form/home.php)
what i want is like what facebook does when we type facebook.com (already logged in before) we r redirected to the news feed ... but in my case i get redirected to Login page(even if i'm already logged in) so i have to login again or type the url to get to the home page
I don't know how to customize sessions to always redirect to home page whenever user is logged in. It should not take me to login page in any condition.
Home page looks like this:
<!DOCTYPE html>
<html>
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<head>
<title>My first PHP Website</title>
</head>
<body>
<h2 align="center">Home Page</h2>
<?php
session_start();
if(isset($_SESSION['user'])){
}else{
header("location: index.php");
}
$user = $_SESSION['user'];
echo "<br>";
echo "WELCOME ".$user." ";
echo "<br>";
echo "Do you want to ";
print 'logout';
echo " ?";
echo "<br>";
?>
</body>
</html>
I rewrote your code a littlebit, so it's more readable now.
First of all, when you want to use header function on your page, or start a session, you should do it before anyhing in the output buffer.
As you see, I removed the echo things, if you use an IDE as an editor, syntax highlighting help you to read the code, and be sure, it is valid.
The second thing, if you want to "remember" for user, when user closes all the browsers what stored the session variables, then you need to give a shot for cookies. But first, fix the code.
<?php
session_start();
if (!isset($_SESSION['user'])) {
header("location: index.php");
}
?><!DOCTYPE html>
<html>
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<head>
<title>My first PHP Website</title>
</head>
<body>
<h2 align="center">Home Page</h2>
<?php
if (!empty($_SESSION['user'])) {
?>
<p>WELCOME <?php echo $_SESSION['user']; ?></p>
<p>Do you want to logout?</p>
<?php
}
?>
</body>
</html>
try this:
<?php
session_start();
if(isset($_SESSION['user'])){
}else{
header("location: index.php");
}
$user = $_SESSION['user'];
echo "<br>";
echo "WELCOME ".$user." ";
echo "<br>";
echo "Do you want to ";
print 'logout';
echo " ?";
echo "<br>";
?>
<html>
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<head>
<title>My first PHP Website</title>
</head>
<body>
<h2 align="center">Home Page</h2>
the problem with your code is that you want the header function after html output

I have to face session issue

I have to face session problem in my new server.
Session not working properly for this link
I have refreshed page not show session and then again refresh page with Ctrl+f5 then session show.
So please tell me this is a server issue or coding issue ??
have a coding issue than solution me any one.
visit link to see phpinfo() : http://brahmanparivar.com/phpinfo.php
index.php and display.php
<?php session_start(); ?>
<html>
<head>
<title>PHP session</title>
</head>
<body>
<? if(isset( $_SESSION['name'] ) ){
echo $_SESSION['name'];
} else{
echo "not have a Session";
} ?>
<br><br>
home display set Session Session Out
</body>
</html>
logout.php
<?php session_start(); ?>
<html>
<body>
<?php
if(isset( $_SESSION['name'] ) ){
session_destroy();
}else{
echo "not have a Session";
}
?>
<br><br>
home display set Session Session Out
</body></html>
set.php
<?php session_start();
$_SESSION['name']="Session Set"; ?>
<html>
<head>
<title>PHP session</title>
</head>
<body>
<? if(isset( $_SESSION['name'] ) ){
echo "Set a Session :: Session Set";
} ?>
<br><br>
home display set Session Session Out
</body>
</html>
in your logout.php page, you are missing a php open tag <?php.
Change this:
<? if (isset($_SESSION['name'])) {...
to this:
<?php if (isset($_SESSION['name'])) {...
I dont think short tag fixes his problem. When i ran your code it works perfectly.
Probably it's configuration issue.
Please read: How to get my session to write to apache
see if that fixes your problem for further trouble shooting
I would like to see the phpinfo();
also attach
<?php
session_start();
echo session_id();
?>
to every page I dont think your PHPSESSIONID is not being set. properly

php redirect when wrong password

is this right the code will redirect a person to the login page when they try to access it using without going into the login page
<?php
$pass = 'password';
?>
<html>
<head>
<title></title>
</head>
<body>
<?php
if ( $_POST["pass"] == $pass){
?>
Congrats you have log in!
<?php
}else{
header("Location: http://signin.com/");
}
?>
</body>
</html>
i ended up having a "Server error
The website encountered an error while retrieving http://www.test.com It may be down for maintenance or configured incorrectly."
You can't call header after you've already outputted some HTML. Do your password checks & redirect. above the HTML
Eg:
<?php
$pass = 'password';
if ( $_POST["pass"] != $pass){
header("Location: http://signin.com/");
exit;
}
?>
<html>
<head>
<title></title>
</head>
....
So the HTML will only show if they're successful.
You can't send a header() after any output to the user:
<?php
$pass = 'password';
if ( $_POST["pass"] == $pass)
{
?>
<html>
<head>
<title></title>
</head>
<body>
Congrats you have log in!
</body>
</html>
<?php
}
else
{
header("Location: http://signin.com/");
}
?>
Something like this would work better:
<?php
$pass = 'password';
if ($_POST["pass"] != $pass){
header("Location: http://signin.com/");
exit;
}
?>
<html>
<head>
<title></title>
</head>
<body>
Congrats you have log in!
</body>
</html>
You need to check if the user is logged in. If not, redirect and exit. If so, display the message.
Put ob_start(); at the top and ob_end_flush(); and that might fix it.
You can't output html before make a redirect with header. Code all logic before:
<?php
$pass = 'password';
if ($_POST["pass"] == $pass)
{
$message = "Congrats you have log in!";
}
else
{
header("Location: http://signin.com/");
}
?>
<html>
<head>
<title></title>
</head>
<body>
<?php echo $message; ?>
</body>

HTTP redirect with header function does not work

header("Location: …" ); does not seem to be working:
<?php
session_start();
if (isset($_SESSION['user'])){
?>
<html>
<head>
<title>
Admin Panel
</title>
</head>
<body>
</body
</html>
<?php
} else {
header("Location: http://echo2.site40.net/cms/admin/login.php" );
}
?>
Seems to be working if I exactly copy paste the code...
Still look for the empty white-spaces before start of the <?php

"HTTP_USER_AGENT" not working. nothing is displayed in my browser i.e. firefox

<?php
$agent=getenv("HTTP_USER_AGENT");
if(preg_match("/MSIE/i", "$agent")){
$result="You are using Microsoft Internet Explorer.";}
else if (preg_match("/Mozilla/i", "$agent")){
$result= "You are using Firefox.";}
else {$result = " you are using $agent.";}
?>
<html>
<head>
<title>Browse Match Results</title>
</head>
<body>
<?php "<p>$result</p>";?>
</body>
</html>
Might I venture a guess, and suggest that:
<?php "<p>$result</p>";?>
Should be:
<?php echo "<p>$result</p>";?>
<?php echo "<p>$result</p>";?>

Categories