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.
Related
login.php
<?php
ob_start();
session_start();
include '../config.php';
if( (isset($_SESSION['can-id']) )) {
header('location: ../home/profile.php');
}
if(isset($_POST['can-login']))
{
$email=$_POST['email'];
$password=$_POST['password'];
$sql="SELECT * FROM `user_credentials` WHERE `email`=:email AND `password`=:password";
$pdoResult=$conn->prepare($sql);
$pdoExec=$pdoResult->execute(array(":email"=>$email,":password"=>$password));
$pdoResult->setFetchMode(PDO::FETCH_ASSOC);
$count=0;
$uid='';
while ($r=$pdoResult->fetch()) {
# code...
$count+=1;
$uid=$r['email'];
}
if ($count==1) {
# code...
$_SESSION['can-id']=$uid;
header('location: ../home/profile.php');
}
else
{
$_SESSION['error']="login failed";
}
}
?>
<html>
....
</html>
profile.php
<?php
ob_start();
session_start();
if (!(isset($_SESSION['can-id']))) {
# code...
header('location: ../login/');
}
else
{
$cid=$_SESSION['can-id'];
}
?>
<h1 ><?php echo $cid;?></h1>
This is my code after log in the page was redirected to profile.php page but in profile page session variable doesn't printed I don't know why but this problem was not occuring every time I log in It occurs sometimes so I can't find what is the problem. Anyone knows please help me to solve the problem.
Remove ob_start() from your login.php
Don't put session_start() in all of your file
e.g login.php, profile.php, etc
but instead, add this to your config.php for example:
<?php
session_start();
//.. config variables here
Then, include config.php also in your profile.php.
My logout.php file is like this. Is there any mistake in my code
logout.php
<?php
session_start();
session_destroy();
header('Location:index.php');
exit;
?>
Here is my index.php file. If I am set $_SESSION['s_activId'] then it is working properly but when I am trying to put condition if $_SESSION['s_activId'] is not set at that time I want to pass header on index page sometimes it works sometimes it does not work.
<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId']))
{
$_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
header("Location:index.php");
}
else
{
$wrong = '';
if(isset($_POST['submit']))
{
$checkLogin = "SELECT userName,password,userType
FROM user
WHERE BINARY userName = '".$_POST['userName']."'
AND BINARY password = '".$_REQUEST['password']."'";
$checkLoginresult = mysql_query($checkLogin);
if($userLoginRow = mysql_fetch_array($checkLoginresult))
{
$_SESSION['s_activId'] = $userLoginRow['userName'];
$_SESSION['s_password'] = $userLoginRow['password'];
$_SESSION['hg_userType'] = $userLoginRow['userType'];
if(!$_SESSION['s_urlRedirectDir'])
{
header("Location:index.php");
}
else
{
header("Location:reminder.php");
}
}
else
{
$wrong = "UserId And Password Is Not Valid";
}
}
}
include("bottom.php");
$smarty->assign('wrong',$wrong);
$smarty->display("index.tpl");
?>
The problem arise in the condition below in index.php:
if(!isset($_SESSION['s_activId']))
{
$_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
header("Location:index.php");
}
When you logout, you are calling session_destroy() on logout.php and redirecting on index.php and the condition above gets true as s_activId is not set in session and again you are redirecting on index.php (without setting s_activId in session). The above condition will be true until the variable s_activId set in session and because of this you are getting ERR_TOO_MANY_REDIRECTS error.
The solution is, on index.php set the variable s_activId in session before calling the header method. Refer the code below:
if(!isset($_SESSION['s_activId']))
{
$_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
$_SESSION['s_activId'] = true;
header("Location:index.php");
}
Dont redirect index.php to index.php. you having redirects loop. Also
if you have code below that also can fire add die in if because after
redirect code below still executes. I didnt read your code, maybe
there isnt problems with this but after
header("Location: lalala"); always add die(); or exit();
I am trying to redirect my php login page so that if user is authorised, it goes to a page (r_index.php) and if the user isn't authorised they go back to the login page (login.html).
This is my code:
<?php
if ("password"=="$password") { // Start the condition ?>
Manage classes
<?php } // End the condition ?>
<?php if ("password"=="") { ?>
Login
<?php }
?>.
What am I doing wrong? How should I resolve it?
replace your code with this:
<?php
if ("password"== $password) {
header("location:r_index.php");
}
else if ($password=="") {
header("location:login.html");
}
?>
If you want to redirect you should use:
header('Location: http://www.example.com/r_index.php');
in your code.
<?php
$accessGranted = false;
if($password == 'password') {
$accessGranted = true;
}
if($accessGranted) {
header('Location: r_index.php');
}
else {
header('Location: login.html');
}
exit;
Actually your syntax is wrong, else there is no problem of using HTML inside php. It will work well and good.
Just make sure not to put your variable inside quotes, and change the statement as follows:
if($password=="password")
and
if($Password==" ")
I want to include a link to my logout.php which includes code to destroy the session inside my php on this page how would I do this?
<?php
if (isset($_SESSION['username'])) {
echo 'Welcome';
"Logout";
} else {
include 'loginform.php';
echo 'Please Log In';
}
?>
Can you try this code, if the SESSION is empty:
<?
session_start();
if (empty($_SESSION['username'])){
echo 'Please Log In';
include 'loginform.php';
}else{
echo 'Welcome';
?>
Home
<?
}
?>
Before you use any $_SESSION variable, you have to call session_start()
The logout should first open the session by calling session_start(), then destroy it by calling session_destroy
You aren't currently telling PHP to display the link to the logout page. It either needs to be included in the echo or outputted elsewhere. For example;
if (isset($_SESSION['username'])) {
echo 'Welcome';
echo 'Logout';
}
else {
include 'loginform.php';
echo 'Please Log In';
}
In your logout.php put this code ,
<?php
session_start();
session_destroy();
?>
I'm trying to use the code below to make the <a href='http://www...com/.../footervote.php'>Vote</a> link appear if a user logs in and a user shows up in the function getEditorsList(). The vote link only appears if the browser is refreshed.
Any idea how I could make the vote link appear without having to refresh the browser?
Thanks in advance,
John
index.php:
<?php
require_once "header.php";
//content
include "login.php";
// more content
require_once "footer.php";
?>
In header.php:
<?php
error_reporting(0);
session_start();
require_once ('db_connect.inc.php');
require_once ("function.inc.php");
$seed="0dAfghRqSTgx";
$domain = "...com";
$editors = getEditorsList();
foreach($editors as $editor)
{
$editorids[] = $editor['loginid'];
}
if(in_array($_SESSION['loginid'], $editorids))
{
echo "<div class='footervote'><a href='http://www...com/.../footervote.php'>Vote</a></div>";
}
?>
login.php:
<?php
if (!isLoggedIn())
{
if (isset($_POST['cmdlogin']))
{
if (checkLogin($_POST['username'], $_POST['password']))
{
show_userbox();
} else
{
echo "Incorrect Login information !";
show_loginform();
}
} else
{
show_loginform();
}
} else
{
show_userbox();
}
?>
Do you set $_SESSION['loginid'] after your in_array query? If you render header.php first, in_array returns false (although the session has been started, but loginid will be set a few lines down the road in login.php).
Move this:
if(in_array($_SESSION['loginid'], $editorids))
{
echo "<div class='footervote'><a href='http://www...com/.../footervote.php'>Vote</a></div>";
}
from header.php to login.php like this:
else {
show_userbox();
if (in_array...
}
If the link is present but hidden you use some DHTML (JQuery / Scriptaculous) to set the display/visibility attributes correctly.
If the link is not present in the original html (preferable for security reasons) then when the login occures fire off an AJAX request that returns javascript that will insert the link in the correct location (parent element).