I have to face session issue - php

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

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

header:location not working

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)){

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

Session data working local but not on remote server

this is my code. plain and simple.
1) first.html
<body>
<?php
session_start();
...
$somearray = $Object->method($somevar);
$_SESSION["somearray"] = $somearray;
...
?>
</body>
1) second.html
<body>
<div id="map_canvas">
<script language="javascript" type="text/javascript">
<?php session_start(); ?>
some_render_function(<?php echo json_encode($_SESSION["somearray"]); ?>);
</script>
</div>
</body>
perfectly working code on localhost.
There are 2 facts that can help you guys to come up with where's the problem here.
1)If you check the source of the page second.html offline and online you can respectively see some_render_function('all the stuff from the json') and some_render_function(NULL)
2)If i check my shared server folder i can see a directory called php_session with apparently all the correct files in it (of all the sessions opened when i tested my project, with CORRECT data in it)
Any hints?
Hello session_start ( http://php.net/manual/en/function.session-start.php ) should always be the first parameter on your page ...
Example
First Page
<?php session_start();?>
<html>
<head>
<title>First</title>
</head>
<body>
<?php
$somearray = $Object->method($somevar);
$_SESSION["somearray"] = $somearray;
?>
</body>
</html>
Second Page
<?php session_start(); ?>
<html>
<head>
<title>Second</title>
</head>
<body>
<?php
var_dump($_SESSION["somearray"]);
?>
</body>
</html>
Session_start should be before any output. So, move <?php to the start of file. Otherwise, behaviour depends on server configuration.

Categories