php session not working for popup login [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am not good at this and have been trying but its not working. I don't know why the get.php is index page and its file header.php has another file login.php. I don't know why the session is not working the way I think it should. Footer should show username when logged in but it doesn't.
get.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css" media="all">
</head>
<body>
<?php include("header.php");?>
<?php include("menu.php");?>
<?php include("slider.php");?>
<?php include("content.php");?>
<div id="footer">
<?php
if(isset($_SESSION['currentuser'])==true)
{
echo"$username";
}
else
{
echo" not logged in ";
}
?>
</div>
</body>
</html>
login.php
<?php
session_start();
include("connect.php");
if(isset($_POST['login']))
{
$username=$_POST['username'];
$password=$_POST['password'];
$query="select * from user where username='$username' AND password='$password'";
$run=mysql_query($query);
if(mysql_num_rows($run)>0)
{
$_SESSION['currentuser']=true;
header("Location:get.php");
}
else
{
header("Location:get.php#loginfail");
}
}
?>

Try this:
if(isset($_SESSION['currentuser']) && $_SESSION['currentuser'] == true)
Also remember to include session_start() on each page which requires sessions.

Sessions can only be accessed after a page refresh when they are set. You'll need to reload the page to be able to access the session variables.
Another note: Everywhere you want you use any kind of session, you'll need to start the session with:
session_start();
So your get.php file would look something like this:
<?php session_start(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css" media="all">
</head>
.....etc

Related

Run php function from index.php

This is on file : PurchaseSiteLoggedIn.php
<!DOCTYPE html>
<html lang="en">
<header>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Palm User Login-Registration</title>
<link rel="stylesheet" href="">
</header>
<script src=""></script>
<body>
<?php
session_start();
if(isset($_SESSION['id']) && isset($_SESSION['username'])){
}
else{
header("Location: http://localhost/loginregister.html");
}
?>
</body>
</html>
If the user is not logged in he/she will be redirected to another page.(the loginregister.html)
This code works fine. What I wanna do is replace:
<?php
session_start();
if(isset($_SESSION['id']) && isset($_SESSION['username'])){
}
else{
header("Location: http://localhost/loginregister.html");
}
?>
with DoAnonymousCheck(); (a random name for the function) so that the code looks cleaner
//
Ideally i would want to have the body of the DoAnonymousCheck on a different file.
I tried somthing like:
I added
<script src='DoAnonymCheck.php'></script>
in the PurchaseSiteLoggedIn.php folder.
And in another folder that i called DoAnonymCheck.php I had
<?php
function DoAnonymCheck(){
session_start();
if(isset($_SESSION['id']) && isset($_SESSION['username'])){
}
else{
header("Location: http://localhost/loginregister.html");
}
}
?>
It didnt work though (i guess in <script src></script> you can only add a .js folder)
You can't "import" a PHP script with a script tag, because all PHP code is executed on the server side before it shows up in the client browser. However, you can use include or require to load other PHP scripts. Code Example

increment button in php using session does not work [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 10 months ago.
This is my PHP code. My backwardCallFunction and forwardCallFunction do not work. The number displayed in:
<div class= 'call-count'>Call Count: <?php echo $_SESSION['callCount'];?></div>
Still the same. Anyone can help me solve the problem?
<?php
session_start();
$_SESSION['callCount'] = 0;
function forwardCallFunction()
{
$_SESSION['callCount']++;
}
function backwardCallFunction()
{
$_SESSION['callCount']--;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<link rel="stylesheet"
href="https://fonts.sandbox.google.com/css2?family=Material+Symbols+Rounded:opsz,wght,FILL,GRAD#20..48,100..700,0..1,-50..200"/>
<title>Document</title>
</head>
<body>
<div class='home-middle-subcontent'>
<div class='to-left' onclick="<?php backwardCallFunction(); ?>"><span class="material-symbols-rounded">keyboard_double_arrow_left</span>
</div>
<div class='call-count'>Call Count: <?php echo $_SESSION['callCount']; ?></div>
<div class='to-right' onclick="<?php $_SESSION['callCount']++; ?>"><span class="material-symbols-rounded">keyboard_double_arrow_right</span>
</div>
</div>
</body>
</html>
Change
<?php
session_start();
$_SESSION['callCount']=0;
to this
<?php
session_start();
if (!isset($_SESSION['callCount']) {
$_SESSION['callCount']=0;
}
Otherwise you will reset the counter every time you access the page.
Once done, you need to rework the html part of your script (javascript and php are not communicating the way you try to do).
Change
<div class='to-left' onclick="<?php backwardCallFunction(); ?>"><span class="material-symbols-rounded">keyboard_double_arrow_left</span>
</div>
to
<a class="to-left" href="?to-left"><span class="material-symbols-rounded">keyboard_double_arrow_left</span></a>
and
<div class='to-right' onclick="<?php $_SESSION['callCount']++; ?>"><span class="material-symbols-rounded">keyboard_double_arrow_right</span>
</div>
to
<a class="to-right" href="?to-right"><span class="material-symbols-rounded">keyboard_double_arrow_right</span></a>
Using the a tag and skinning it as a DIV with CSS is way simpler (and usual) than the opposite way.
Once you have done, you need to read the user action using the uri you get and perform the needed action:
<?php
function forwardCallFunction()
{
$_SESSION['callCount']++;
}
function backwardCallFunction()
{
$_SESSION['callCount']--;
}
session_start();
if (!isset($_SESSION['callCount']) {
$_SESSION['callCount']=0;
}
$action = $_SERVER['QUERY_STRING'];
if ($action == 'to-left') backwardCallFunction();
if ($action == 'to-right') forwardCallFunction();
?><!DOCTYPE html>
The mechanism of php/javascript interaction is just this simple (ajax obfuscate a bit the process but the way it works is the same):
PHP create a page served to the client browser
the user interacts with Links and Buttons and send data to php via the url query string
PHP read the query string (as I have done or via the $_GET / $_POST / $_REQUEST superglobals)
the PHP script take every action needed to check the user input against input data tampering and user misbehavious (aka XSS attack and similar
PHP create a page served to the client browser and so on

I am not able to set session after login [duplicate]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am not good at this and have been trying but its not working. I don't know why the get.php is index page and its file header.php has another file login.php. I don't know why the session is not working the way I think it should. Footer should show username when logged in but it doesn't.
get.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css" media="all">
</head>
<body>
<?php include("header.php");?>
<?php include("menu.php");?>
<?php include("slider.php");?>
<?php include("content.php");?>
<div id="footer">
<?php
if(isset($_SESSION['currentuser'])==true)
{
echo"$username";
}
else
{
echo" not logged in ";
}
?>
</div>
</body>
</html>
login.php
<?php
session_start();
include("connect.php");
if(isset($_POST['login']))
{
$username=$_POST['username'];
$password=$_POST['password'];
$query="select * from user where username='$username' AND password='$password'";
$run=mysql_query($query);
if(mysql_num_rows($run)>0)
{
$_SESSION['currentuser']=true;
header("Location:get.php");
}
else
{
header("Location:get.php#loginfail");
}
}
?>
Try this:
if(isset($_SESSION['currentuser']) && $_SESSION['currentuser'] == true)
Also remember to include session_start() on each page which requires sessions.
Sessions can only be accessed after a page refresh when they are set. You'll need to reload the page to be able to access the session variables.
Another note: Everywhere you want you use any kind of session, you'll need to start the session with:
session_start();
So your get.php file would look something like this:
<?php session_start(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css" media="all">
</head>
.....etc

How can I give a title for each of my pages?

I would like to give a title to each of my pages, but all my pages are linked to my index.php:
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="UTF-8">
<title>Test</title>
<link href="css/style.css" rel="stylesheet" type="text/css">
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php include("top_bar.php");?>
<?php include("header.php");?>
<?php include("container.php");?>
<?php include("footer.php");?>
</body>
</html>
Here is how my site is: http://prntscr.com/47nn7h
All my pages have the title I put for index.php, but how to add a title to a specific page (example, when I go to the page members.php)?
members.php:
<?php include "index.php";?>
Thanks.
Replace the existing <title> tag with this.
<title><?php echo $pagetitle; ?> </title>
in your <head> block.
Make sure that $pagetitle actually contains the desired title before you emit the tag. It's not clear from your question where these titles are coming from - you'll probably need some PHP right at the top of the page to set all this up.
.
You could use this method and add the
$pageTitle = 'Title of Page';
to your content page (i.e. member page)

Automatically Access Denied, What's Happening?

Ok so I just resolved my last question and I need help AGAIN...
So I fixed the authentication code but now every time I log in to my profile, it says I'm not logged into my site. Here's the access-denied.php code:
<title>Access Denied</title>
<link href="loginmodule.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Access Denied </h1>
<p align="center"> </p>
<h4 align="center" class="err">Access Denied!<br />
You do not have access to this resource.</h4>
And Here's my auth.php code:
//Start session
session_start();
//Check whether the session variable SESS_MEMBER_ID is present or not
if(!isset($_SESSION['email']) || ($_SESSION['password'])){
header("location: access-denied.php");
exit();
}
And here's my profile page code:
<?php session_start(); ?>
<?php include("inc/incfiles/loggedheader.php")?>
<?php
require_once('auth.php');
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Member Index</title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Welcome <? if(isset($_SESSION['SESS_fname'])){
echo $_SESSION['SESS_fname'];
} ?></h1>
My Profile | Logout
<p>Welcome to your profile! </p>
</body>
</html>
Why can't I log into my profile? I know nothing about this type of PHP I'm barely still learning, my friend (who is helping me build my site) got the codes and just emails them to me.
There are problem in line
if(!isset($_SESSION['email']) || ($_SESSION['password'])){
you do need to check second part (password check) of if condition. Here you are checking that if email not exists in session or password is exists in session. So even if email set and then page goto access denied due to second condition is true. If necessary, you should use
if(!isset($_SESSION['email']) || !($_SESSION['password'])){
Here is the problem:
if(!isset($_SESSION['email']) || !isset($_SESSION['password'])){
...
}

Categories