Cannot Modify Header Information Session Cookie Create? [duplicate] - php

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 8 years ago.
Firstly I like to say this is not a duplicate I tried everything on google such as ob_functions and space not luck
PHP should of made this simple but they haven't which is stupid..
Why am I getting this error for just starting a cookie in my script?
I have commented the cookie start I am having the issue with and hope someone can help fix it
I tried and now gave up wasting my time..
<!DOCTYPE html>
<?php
session_start();
?>
<html lang="en" class="no-js">
<head>
<meta charset="utf-8">
<title>Welcome To Spud Gaming</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<!-- CSS -->
<link rel='stylesheet' href='http://fonts.googleapis.com/css?family=PT+Sans:400,700'>
<link rel="stylesheet" href="assets/css/reset.css">
<link rel="stylesheet" href="assets/css/supersized.css?vvssv">
<link rel="stylesheet" href="assets/css/style.css?v12">
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<?php
require "../core/links.php";
if(isset($_SESSION['user'])) {
die;
}
if(isset($_COOKIE['user'])) {
die;
}
?>
<div class="page-container">
<br><br><br>
<h1>Sign In</h1>
<div style='color:black;'>
<?php
if((isset($_POST['username'])) && (isset($_POST['password']))) {
require "../core/data_connect.php";
$username = mysqli_real_escape_string($connection,$_POST['username']);
$password = mysqli_real_escape_string($connection,(sha1(md5($_POST['password']))));
if(($username) && ($password)){
$login_query_string = "SELECT * FROM spud_members WHERE ( username='$username' OR email = '$username') and password='$password'";
$login_query = mysqli_query($connection,$login_query_string);
$login_rows = mysqli_num_rows($login_query);
$login_array = mysqli_fetch_array($login_query);
$mid_login = $login_array['mid'];
$active_login = $login_array['active'];
//if we found a user with that information then sign them in
if($login_rows==1) {
//if the user as not been suspended then start there session
if($active_login==1) {
//if the user has not checked the remember me box
if($_POST['start_cookie_true_checked']!='on') {
$_SESSION['user']=$mid_login;
die;
} else if($_POST['start_cookie_true_checked']=='on') {
//otherwise if the remember me box has been checked then start a cookie instead
$date_of_expiry = time() + 60 ;
//THIS COOKIE HAS A ISSUE AND POPS UP THESE ERRORS!!!!!!!!!!!!!!!!!!!!!!!!!
setcookie("user", 'Mark', time()+3600);
die;
}
} else {
echo "<b>Account Suspended</b><br>
Your Account Has Been Suspended:<br>
This May Be Because You Have Broken Our Terms Of Use Guidelines<br><br>";
}
} else {
echo "<b>Invalid Username/Email Or Password</b><br>
The Username/Email Or Password That You Supplied Is Invalid<br>
Please Try Again or <a href=''> Reset Your Password</a>";
}
} else {
echo "<b>Blank Username/Email And Password</b><br>
You Haven't Entered A Username/Email And Password";
}
}
?>
</div>
<form action='' method='post'>
<input type='text' name='username' placeholder='Username Or Email' size='25' maxlength='30' /><br />
<input type='password' name='password' placeholder='Password' size='25' />
<input type='checkbox' name='start_cookie_true_checked'>
<input type='hidden' name='action' value='do_login'>
<input type='hidden' name='url' value='index.php' /> <br><br>
<style>
a {
color:blue;
text-decoration:none;
}
</style>
<div>
<a href='signup'> Need An Account? </a> | <a href='forgot'> Forgot Password?</a>
</div>
<button type="submit">Sign In</button>
</form>
</div>
<!-- Javascript -->
<script src="assets/js/jquery-1.8.2.min.js"></script>
<script src="assets/js/supersized.3.2.7.min.js"></script>
<script src="assets/js/supersized-init.js?ddsddfd"></script>
<script src="assets/js/scripts.js"></script>
</body>
</html>
Thanks :)

Move HTML outpout after to session_start function.
<?php
session_start();
?>
<!DOCTYPE html>

You can't try to set a cookie (which is done in response headers) after you have started to make output to the page (which starts on your first line of code).

Related

How do I make a value not reset each page reload?

I am trying to show people their profiles from my database using PHP.
But every time a button is clicked I want the person_id ($which_person) to go up by 1 so the next person's profile is shown. I don't know how to do that because every time I click a button the page reloads so the variable "which_person" gets reset to 1.
<!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 href="assets-putinhouse/css/style1.css" rel="stylesheet" type="text/css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Festive&display=swap" rel="stylesheet">
<title>Put people in a house</title>
</head>
<body>
<?php
include "db/connection.php";
$conn = create_connection();
$which_person = 1; //This resets to "1" every time the page reloads
$getSql = "select * from Person where person_id = 1;";
$data_labels = mysqli_query($conn, $getSql)->fetch_all(MYSQLI_ASSOC);
?>
<div class="pagesize">
<h1>Put people in a house</h1>
<img src="assets-putinhouse/images/create_account.png" alt="profilepic" class="image_person">
<?php
foreach($data_labels as $labels)
{
?>
<li class="labels" data-id="<?php echo $labels['person_id'];?>">
<?php echo $labels["firstname"] ?>
<br>
<br>
<?php echo $labels["secondname"] ?>
<br>
<br>
<?php echo $labels["gender"] ?>
<br>
<br>
<?php echo $labels["descriptie"] ?>
</li>
<?php
}
?>
<br>
<form method="GET">
<input type="submit" class="slytherin_button" value="Slytherin" name="slytherin_button_name">
<br>
<input type="button" class="gryffindor_button" value="Gryffindor" name="gryffindor_button_name">
<br>
<input type="button" class="hufflepuff_button" value="Hufflepuff" name="hufflepuff_button_name">
<br>
<input type="button" class="ravenclaw_button" value="Ravenclaw" name="ravenclaw_button_name">
<br>
<input type="button" class="nextperson_button" value="Go to next person">
<p class="text_firstname">Firstname: </p>
<p class="text_name">Name: </p>
<p class="text_gender">Gender: </p>
<p class="text_info">Info: </p>
<?php
if(isset($_GET['slytherin_button_name'])) {
onFunc($conn, $which_person);
}
if(isset($_GET['gryffindor_button_name'])) {
offFunc();
}
function onFunc($conn, $wich_person){
$sqlUpdate = "UPDATE Person SET slytherin = slytherin + 1 WHERE person_id like '$which_person';"; //this does the value "slytherin" of which_person go up by 1;
mysqli_query($conn, $sqlUpdate);
$wich_person = $wich_person + 1; //which person goes up by 1 so the next person will be shown but gets reset at the start
}
function offFunc(){
echo "Button is clicked";
}
?>
</div>
<script src="assets-putinhouse/js/script.js"></script>
</body>
</html>
´´´
(Untested) Instead of:
$wich_person = 1;
It seems you could do something like:
if (isset($_GET["id"]) {
$wich_person = (int)$_GET["id"] + 1;
}
else {
$wich_person = 1;
}
and then add the following to your form:
<form action="?<?php echo $wich_person ?>" method="GET">
...
</form>
And also, unless I'm mistaken it seems you've forgot to add </form> to your code.
In any event, this code should add the current ID to your URL every time you press submit, where then it gets increased by 1.
Edit: This is intended as a quick solution based on the code structure you provided, but upon reflection not sure if it's exactly what you're looking for. More robust solutions could be had, such as cookies or sessionStorage.

solving problems in session? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have created two pages , one is index.php and admin.php . In the first page or index.php , i have created a loggin form so that i can access the admin.php page trough the loggin or like this enter image description here
Now by logging in i go to admin.php page. Here is the question what i want to ask about that now when ever i click the back button or next button in the chrome. I am returning to the admin.php page . I have tryed the session_start() and the if(!isset($password) || !isset($user)){}. But this code for obvious reasons doesnt work . So can someone help me out with this ?
The code for the example is here index.php
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Admin</title>
</head>
<body>
Reset
<div class="image">
<img src="img/adi.png" alt="image"> <!-- image -->
</div>
<form action="inc/login.php" method="POST"><br>
<p class="title">Log In</p>
<label for="Username">User :</label>
<input type="text" name="username" id="user"> <br> <!--username -->
<label for="Password">Password :</label>
<input type="password" name="password" id="password" ><br><br> <!-- password -->
<label for="showpassword" class="showpassword">Show Password</label>
<input type="checkbox" name="checkbox" id="chk" ><br><br> <!-- checkbox -->
<input type="submit" name="submit" value="Log in" > <!-- enter -->
</form>
<?php
if(!isset($_GET['Login'])){
exit();
}else{
$check=$_GET['Login'];
if($check=="userEmpty"){
echo "<p class='class_login'>user is empty</p> ";
}elseif($check=="passwordEmpty"){
echo "<p class='class_login'>password is empty</p> ";
}elseif($check=="wrongUser"){
echo "<p class='class_login'>user is wrong</p> ";
}elseif($check=="Password"){
echo "<p class='class_login'>password is wrong</p> ";
};
} ;
?>
<script src="js/main.js"></script>
</body>
</html>
and the code for the admin.php is this one :
<?php
session_start();
if(!isset($username) || !isset($password)){
header("location:index.php?data=closed");
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<script>
window.history.forward();
</script>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Document</title>
</head>
<body>
<div class="top">
Log Out
</div>
</body>
</html>
Besides using of session_start you need to store something in the session, using something like this:
$_SESSION['userId'] = <value>;
which must be set in your login.php and checked by your admin.php if user has access rigths to visit secured page. I.e. you need to check:
if (isset($_SESSION['userId']) && $_SESSION['userId'] == <value>) {
// access granted
}

PHP Form does not log in and move to next page

I ran into another problem with my website that I can't get around.
I'm trying to make it so when the admin logs in he will be taken to the admin page but for some reason when I enter the correct details into the log in and press the submit button it just brings me back to the admin log in page again.
Here is a Gyazo of my problem
https://gyazo.com/34f133fea4b20ec285ee7ff491053145
<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
<meta charset="UTF-8">
<title>Login</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.min.js" type="text/javascript"></script>
<link href='https://fonts.googleapis.com/css?family=Ubuntu:400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/148866/reset.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<body>
<main id="cd-main-content">
<section id="cd-intro">
<h1>Admin Log In</h1>
<header class="cd-header">
<?php
require('db.php');
session_start();
// If form submitted, insert values into the database.
if (isset($_POST['username'])){
// removes backslashes
$adminusername = stripslashes($_REQUEST['adminusername']);
//escapes special characters in a string
$adminusername = mysqli_real_escape_string($con,$adminusername);
$adminpassword = stripslashes($_REQUEST['adminpassword']);
$adminpassword = mysqli_real_escape_string($con,$adminpassword);
//Checking is user existing in the database or not
$query = "SELECT * FROM `admin` WHERE username='$adminusername'
and password='".md5($adminpassword)."'";
$result = mysqli_query($con,$query) or die(mysql_error());
$rows = mysqli_num_rows($result);
if($rows==1){
$_SESSION['username'] = $adminusername;
// Redirect user to admin page /////////////////////////////////////////////////////////////////////
header("Location: adminpage.php");
}else{
echo "<div class='form'>
<h3>Username/password is incorrect.</h3>
<br/>Click here to <a href='admin.php'>Login</a></div>";
}
}else{
?>
<div class="form">
<form action="" method="post" name="login">
<input type="text" name="adminusername" placeholder="Username" required />
<input type="password" name="adminpassword" placeholder="Password" required />
<input name="submit" type="submit" value="Login" />
</form>
</div>
<?php } ?>
<a class="cd-menu-trigger" href="#main-nav">Menu<span></span></a>
</header>
<div class="cd-blurred-bg"></div>
</section> <!-- cd-intro -->
</main>
<div class="cd-shadow-layer"></div>
<nav id="main-nav">
<ul>
<li><span>Login</span></li>
<li><span>What's On</span></li>
<li><span>Favourites</span></li>
<li><span>About</span></li>
<li><span>Admin</span></li>
</ul>
Close<span></span>
</nav>
</body>
<script src='js/jquery.min.js'></script>
<script src="js/index.js"></script>
</body>
</html>
You are doing:
if (isset($_POST['username'])) {
//...
}
Try this
if (isset($_POST['adminusername'])) {
//...
}
As a note, I suggest you to try use a framework, like laravel.
first I would debug by var_dump($_POST) and see what you get. I would bet that since you dont have an ID set for the input value name it is not working. If anything checking for ISSET($_POST['username']) would never work because you do not have a vairable set for that name.
var_dump($_POST) and see what you get. If not add an ID='username' to the input
try this :-
if (isset($_POST['submit'])){
// rest of code...
}

Server redrict error

index.php
<?php
require 'linessystem/connection.php';
$msg = "";
if (isset($_SESSION['username']) && !empty($_SESSION['username'])) {
if ($_SESSION['level']=='admin' or $_SESSION['level']=='user') {
header("location:linessystem/administrative/index.php");
}
if ($_SESSION['level']=='agent' && !empty($_SESSION['lang'])) {
if ($_SESSION['lang']=='ar') {header("location:linessystem/agent/ar/index.php");}
if ($_SESSION['lang']=='hr') {header("location:linessystem/agent/hr/index.php");}
if ($_SESSION['lang']=='en') {header("location:linessystem/agent/en/index.php");}
}
}
if (isset($_POST['username']) && !empty($_POST['username'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$query="select * from users where username='$username' AND password='$password'";
$result = mysqli_query($conn,$query) or die(mysql_error($conn));
$login = mysqli_num_rows($result);
$row = mysqli_fetch_assoc($result);
if($login>0){
$_SESSION['username'] = $username;
$_SESSION['lang']=$row['language'];
$_SESSION['agent_id']=$row['agent_id'];
$_SESSION['level']=$row['level'];
if (isset($_SESSION['username']) && !empty($_SESSION['username'])) {
if ($_SESSION['level']=='admin' or $_SESSION['level']=='user') {
header("location:linessystem/administrative/index.php");
}
if ($_SESSION['level']=='agent' && !empty($_SESSION['lang'])) {
if ($_SESSION['lang']=='ar') {header("location:linessystem/agent/ar/index.php");}
if ($_SESSION['lang']=='hr') {header("location:linessystem/agent/hr/index.php");}
if ($_SESSION['lang']=='en') {header("location:linessystem/agent/en/index.php");}
}
}
}else{$msg= ' <div align="center" style="color:red;font-weight: bold;font-size:15px;"> wrong username or password </div><br>';
}}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Bootstrap -->
<link href="assets/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="assets/css/login-style.css">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="login-page">
<div class="form" align="center" >
<form class="login-form" method="post" action="index.php" id="loginid" >
<input type="text" name="username" required="" placeholder="username"
autocomplete="off">
<input type="password" name="password" required="" placeholder="password"
autocomplete="off ">
<p class="message"><?php echo $msg; ?></p>
<button>login</button>
</form>
</div>
</div>
</body>
</html>
I have an error in the server code of above one. I bought adomain and hosting and wh web siter is run already in other server but in this new hosting isnt run when it reserve the value but don't redirect me to the main page and the url of new hosting is http://www.quicklineltd.com.
where is the problem that I could not make it ?
User name is 2 and pass is 2.
Do you even make it into the if statement? Try an echo instead of header.
And did you use a session_start(); at the beginning of your php file?
Or did you include the file above inside another file wich already echo'd some output, because then header wont work either.
Or you should have to put a slash in front of the path, or even use a full url as in http://
Please use session_start(); at the beginning of the file. Without starting the session, the sessions used in the file will not work.

page does not load when it is online php

I have a page which loads completely when it is on a local server i uploaded the site on the server and some of my pages won't load here is the example of my pages
should i do something more when it is on server?what is the common problems in this situations?
<?php include("includes/manager_session.php");?>
<?php require_once("includes/connection.php");?>
<?php
$message="";
if(isset($_POST['submit'])){
$uname= mysqli_real_escape_string($cnn,$_POST['uname']);
$pass= mysqli_real_escape_string($cnn,$_POST['pass']);
$hashed_pass=sha1($pass);
function redirect_to($location){
header('Location:'.$location);}
//checking for available users
$query="select manager_id,uname,hashed_pass from manager where uname='{$uname}' and hashed_pass='{$hashed_pass}'";
$result=mysqli_query($cnn,$query);
//if query returns a thing
if($result){
$num_rows = mysqli_num_rows($result);
//agar user peida shod
if($num_rows == 1){
$found_user=mysqli_fetch_array($result);
$_SESSION['manager_id']=$found_user['manager_id'];
$_SESSION['manager_pass']=$found_user['hashed_pass'];
redirect_to("manager_profile.php");
}
//useri peida nashod
else{
echo "can not find user";
redirect_to("manager_login.php");
}
}else{echo mysqli_error($cnn);}
}
?>
<html lang='fa'>
<head>
<meta charset="utf-8">
<title>
کیمیافکر
</title>
<link rel="stylesheet" type="text/css" href="stylesheets/login-form.css" />
</head>
<body>
<div id="wrapper">
<form name="login-form" class="login-form" action="" method="post">
<input type="text" name="uname"/>
<input type="pass" name="pass"/>
</div>
</body>
</html>
<?php
mysqli_close($cnn);
?>
all pages that has the session part comes up with nothing in it (on of the sessions) when it is online so what is the problem her am i missing something ?

Categories