Jump to content

Help me with a header plz


k3n3t1k

Recommended Posts

Could you look at this code and tell me what you think would cause the page not to foward to the header?

 

 

<?php
session_start(); ?>
<html>
<head>
<title>
Login
</title>
<body>
<?php
require_once "dbconfig.php";

$sql = mysql_query("SELECT * FROM studentcenter WHERE username='"($_POST['username'])."'")
or die("Username is Incorrect  ".mysql_error());
/* Check if Username Exists */

$results = mysql_fetch_array($sql)
or die("Error at results section: ".mysql_error());
/* Puts database information into an array */

if($result['password'] == $_POST['password']) {
/* Checks if passwords match */

/* Start Session */
header ("Cache-control: private");
$SESSION['sessionname'] = $_POST['username'];
session_name($SESSION['sessionname);
session_id([id]);
echo("Welcome, Forwarding");
header("location: studenthome.php?id")
or die("Will not foward" .mysql_error());
}else{
echo "Incorrect Login, please try again or <a href="register.php">Register</a>";
}
?>
</body>
</html>

 

Thanks,

k3n3t1k

Link to comment
https://forums.phpfreaks.com/topic/77366-help-me-with-a-header-plz/
Share on other sites

<?php
header ("Cache-control: private");
$SESSION['sessionname'] = $_POST['username'];
session_name($SESSION['sessionname']);
session_id([id]);
echo("Welcome, Forwarding");
header("location: studenthome.php?id")
or die("Will not foward" .mysql_error());
}else{
echo "Incorrect Login, please try again or <a href="register.php">Register</a>";
}
?>

Well, first off you have a syntax error that darkfreaks fixed.

 

Secondly, you're using the wrong variable for sessions; try $_SESSION instead of $SESSION.

 

Thirdly, you are echo'ing output before your call to header().  You must never do this.  Headers have to be sent before any output; nothing can come before headers, not even white space.  You'll have to restructure your logic to fix that.

 

Fourth, if you want to redirect with a header('Location: ...') then you must call exit(); afterwards or your script will keep processing.

Is this what it should look like after those errors?

 

<?php
header ("Cache-control: private");
$_SESSION['sessionname'] = $_POST['username'];
session_name($SESSION['sessionname']);
session_id([id]);
header("location: studenthome.php?id");
exit();
echo("Welcome, Forwarding");
or die("Will not foward" .mysql_error());
}else{
echo "Incorrect Login, please try again or <a href="register.php">Register</a>";
}
?>

<?php
header ("Cache-control: private");
$_SESSION['sessionname'] = $_POST['username'];
session_name($_SESSION['sessionname']);
session_id([id]);
header("location: studenthome.php?id");
exit();
echo("Welcome, Forwarding");
or die("Will not foward" .mysql_error());
}else{
echo "Incorrect Login, please try again or <a href="register.php">Register</a>";
}
?>

 

still forgot the underscore in session  :P

<?php
header ("Cache-control: private");
$_SESSION['sessionname'] = $_POST['username'];

// In the following line you may mean $_SESSION['sessionname'], although I'm not sure
session_name($SESSION['sessionname']);

// What in the Hell is [id]?
session_id([id]);

// It doesn't look to me as if you finished your query string...?id=the_id
header("location: studenthome.php?id");
exit();

// Next line is pointless, you'll have already exited and redirected (you'll never see the message)
echo("Welcome, Forwarding");

// What is this or die() attached to?
or die("Will not foward" .mysql_error());

// Is there more code above this? or do you just have a random }else{ floating around
}else{
echo "Incorrect Login, please try again or <a href="register.php">Register</a>";
}
?>

I tried the suggestions above to no avail.

 

I probabbly should have posted this before but there is more code, here is the entire page:

 

<?php
session_start(); ?>
<html>
<head>
<title>
Login
</title>
<body>
<?php
require_once "dbconfig.php";

$sql = mysql_query("SELECT * FROM studentcenter WHERE username='"($_POST['username'])."'");
/* Check if Username Exists */

$results = mysql_fetch_array($sql);
/* Puts database information into an array */

if($result['password'] == $_POST['password']) {
/* Checks if passwords match */

/* Start Session */

header ("Cache-control: private");
$_SESSION['sessionname'] = $_POST['username'];
session_name($_SESSION['sessionname']);
session_id(1);
header("location: studenthome.php?id=1");
exit();
}else{
echo "Incorrect Login, please try again or <a href="register.php">Register</a>";
}

?>
</body>
</html>

You are sending output before your calls to header().

 

<?php
session_start(); ?>
<html>
<head>
<title>
Login
</title>
<body>

 

That markup counts as output.  I already told you that you need to restructure your code to avoid that.

I put the both headers before anything. It still wont foward.

 

<?php
header("location: studenthome.php?id=1");
header ("Cache-control: private");
session_start();



require_once "dbconfig.php";

$sql = mysql_query("SELECT * FROM studentcenter WHERE username='"($_POST['username'])."'");
/* Check if Username Exists */

$results = mysql_fetch_array($sql);
/* Puts database information into an array */

if($result['password'] = $_POST['password']) {
/* Checks if passwords match */

/* Start Session */


$_SESSION['sessionname'] = $_POST['username'];
session_name($_SESSION['sessionname']);
session_id(1);
/* header("location: studenthome.php?id=1"); */
exit();
}else{
echo "Incorrect Login, please try again or <a href="register.php">Register</a>";
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.