PHP 'stops' (is empty) after session_start(); - php

I have a problem using the php method session_start();
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
session_start();
echo "Sesstion Started";
}
This is at the very beginning auf the php-file. If I execute this on my webserver and the requested method is POST, the page stays empty and it seems as if nothing is executed after the session_start();
Does anyone have an idea why this happens?
Cheers,
Cheeesi

If it were me running into this issue I would simply do this;
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
session_start();
echo "Sesstion Started";
}
else
{
echo "Error : Session NOT Started";
}
?>
This would help with the "empty" output until you can track down the error reporting.

Related

Why Redirect to webpage not found?

I'm redirecting to page by link "welcome.php?language=english".
But when I place function 'confirm_patient_logged_in()' below Session declaration it's showing "Website not found". while it redirects to 'login.php' when this function is placed above Session declaration.
Can anyone tell what is reason behind this?
Welcome.php
<?php require_once("../../includes/session.php"); ?>
<?php confirm_patient_logged_in(); ?> //WORKING HERE
<?php
if(isset($_GET["language"])){
if($_GET["language"] == "english"){
$_SESSION["language"] = "english";
}else{
$_SESSION["language"] = "hindi";
}
}
?>
<?php confirm_patient_logged_in(); ?> // NOT WORKING HERE
Function:
function confirm_patient_logged_in(){
if(!patient_logged_in()){
redirect_to("login.php");
}
}
function redirect_to($new_location){
header("Location: " . $new_location);
exit;
}
patient_logged_in() checks if user is logged-in or not
It seems like you forgot to add session_start(); before using $_SESSION variable:
<?php
require_once("../../includes/session.php");
session_start();
if(isset($_GET["language"])){
if($_GET["language"] == "english"){
$_SESSION["language"] = "english";
}else{
$_SESSION["language"] = "hindi";
}
}
confirm_patient_logged_in(); // SHOULD WORK
?>
So it the missing session_start(); was causing an error but you were unable to see the error probably display_error ini php.ini is disabled and you were getting Website not found notification on your browser.

condition to check if the session variables as well as get and post have been destroyed in php

The code is like:(It is the last page of the web-app I have made)
<?php
if(isset($_GET['var'])
{
session_start();
$a=$_SESSION['prev_defined'];
#more use of session variable
session_destroy();
$_SESSION = array();
unset($_GET);
unset($_POST);
}
?>
Now when i execute the web application it runs fine , when i refresh the last page whose code is given above the warning message shows of undefined symbol because the $_SESSION variables as well as $_GET and $_POST have been deleted. I want to display message "SESSION OVER" on refresh. How to do it? Where to put if condition? I have tried to put the above code in
if($_SESSION)
{
#entire code above
}
else
{
echo"SESSION OVER";
}
but it displayes message undefined variable _SESSION
<?php
if(isset($_GET['var'])
{
if(isset($_SESSION))
{
session_start();
$a=$_SESSION['prev_defined'];
#more use of session variable
session_destroy();
$_SESSION = array();
unset($_GET);
unset($_POST);
}
else
{
echo"SESSION OVER";
}
}
?>
Try this one. If session is set it will do the conditions.
EDIT
if(isset($_GET['var'])
{
....
}
else
{
echo"SESSION OVER";
}
Using this, If $_GET['var'] is not set then echo the else part
EDIT 2
<?php
if (isset($_GET) || isset($_SESSION)) {
//Put all your codes here
} else {
echo "Session Over";
}
$_SESSION is a global variable, an array. And it will be allways around.
check if session feature is active like so:
if (session_status() == PHP_SESSION_NONE) {
session_destroy();
echo "session over";
}
Also note to check if arrays key isset, before checking it's value, to avoid notices:
if(isset($_SESSION['login'])&&$_SESSION['login']==1){//pass}
EDIT:
as stated here: For versions of PHP < 5.4.0
if(session_id() == '') {
session_start();
}

Need help making an if statement in php that creates a html href link when passed correctly

<?php if ($_SESSION["isLoggedOn"] == True) {?>
LOGOUT
<?php }?>
i want the link to only be created if the IF statement is successful and to not create it if it is not. At the moment no matter what i have tried to change the link will always be created. thanks.
Have you tried
<?php
if ($_SESSION["isLoggedOn"] == True)
{
echo 'LOGOUT';
}
?>
Your this visible code seems to be corrected. May be you would have some error in storing the sessions. Try:
echo $_SESSION['isLoggedOn'];
OR try doing this:
<?php
if ($_SESSION['isLoggedOn']) {
echo "Logout";
}?>
<?php if (isset($_SESSION["isLoggedOn"]) && $_SESSION["isLoggedOn"] == True) {?>
LOGOUT
<?php }?>
Your $_SESSION["isLoggedOn"] is always TRUE.
Also dont use == TRUE for True/False variables.
Also logging out at terms and conditions page is not very good i guess ;-)
Elaborate your code:
<?php
session_start();
if ( !isset($_SESSION) || !isset($_SESSION["isLoggedOn"]) {
echo "an error occured instantiating the session.";
exit 1;
}
if ( $_SESSION["isLoggedOn"] === true ) {
echo 'LOGOUT';
}
?>
You need to start a session as very first action in your PHP.
Check if you actually have a vale in $_SESSION
Then, check its state and output what you want.

PHP after redirect the session variable changes to false

I have created a login screen that checks if the password is correct.
After submitting the login form I get to process.php that has these lines:
if (password_verify($passwordPost, $passwordDB)) {
$_SESSION['loged_in'] = true;
} else {
$_SESSION['loged_in'] = false;
}
# when I do a print_r on $_SESSION['loged_in'] it results true
header('Location: ../../admin/index.php');
The index page that checks the session (../../admin/index.php)
<?php
session_start();
# when I do a print_r on $_SESSION['loged_in'] here, it results false
if ($_SESSION['loged_in'] == false) {
include(PATH_COMPONENTS.'login/index.php');
}
?>
How is this possible?
You have to start the session in every file! Like this:
session_start(); //most times at the top of every file
YES, you have to start the session in every file your using it!
Also for error reporting use this:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
?>

unset variable done before it should unset

I have this peace of code of which I don't understand why it's not working as I expect.
<?php
session_start();
if($_SERVER['REQUEST_METHOD'] == "POST") {
$_SESSION['reg'] = "done";
header("Location: " . SELF, true, 302);
}
if((isset($_SESSION['reg']) AND ($_SESSION['reg'] == "done"))) {
unset($_SESSION['reg']);
echo "Done";
}else{
echo "Not done";
}
?>
After POST it redirects and echo's Not done but I would expect it would echo's Done. If I remove the line with unset, it works fine and echo's Done.
This isn't the behavior I would expect. What mistake am I making?
A header redirect does not stop execution of the script, so the unset is carried out immediately.
To fix exit after the redirect
<?php
session_start();
if($_SERVER['REQUEST_METHOD'] == "POST") {
$_SESSION['reg'] = "done";
header("Location: " . SELF, true, 302);
exit();//<-- add this
}
if((isset($_SESSION['reg']) AND ($_SESSION['reg'] == "done"))) {
unset($_SESSION['reg');
echo "Done";
}else{
echo "Not done";
}
?>
write this unset($_SESSION['reg']); instead of this unset($_SESSION['reg');

Categories