visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: PHP :: login script not redirecting after successful login.


By: PHP newbee U.S.A.  Date: 17/04/2003 00:00:00  English  Points: 100 Status: Answered
Quality : Excellent
heres my code:

$login = $_POST["login"];
$pass = $_POST["pass"];
require 'variables.php';

if ($_POST["submit"]) {
$dbconnect = mysql_connect("$db_host","$db_user","$db_pass") or
die ("Could not Connect to MySQL. " . mysql_error());
mysql_select_db("$table") or
die ("Could not connect to $db_name. " . mysql_error());
$query = "SELECT * FROM main WHERE login = '$login' AND pass = '$pass'";
$result = mysql_query($query);
$check = mysql_num_rows($result);
mysql_close($db_connect);
if($check > 0) {
echo "success!";
session_start();
session_register("login");
header("location:index.php?".session_name()."=".session_id());
} else {
echo "Error! wrong login or pass!";
}
}

it checks and works fine, even registers the session, but it wont redirect!
By: VGR Date: 17/04/2003 04:06:00 English  Type : Answer
that because of the echo "success!"

remove or comment the line

header() can not work if ANY output has begun (1) so not even a single space must precede the header("Location: gnagnagna") statement





(1) bemol : ob_* functions)
By: VGR Date: 17/04/2003 04:09:00 English  Type : Comment
I also suggest you redirect in cas of negative login : ther user doesn't have to stay on that silly page ;-)
(unless the FORM to login is just after the if ($_POST["submit"]) block, of course


also note that you session_register("login") so basically you're usinbg "the old way" (like lme :D ) : you should have put a value in the variable $login before registering it :D

The "new way" would just be :
$_SESSION['login']=value; // here you see that a value is required to set a sesison variable :D
By: PHP newbee Date: 17/04/2003 06:38:00 English  Type : Comment
ok, I took out the echo and took out that tab, but it still doesnt work. thats becuase of all the other junk above it? is there an alternative?
By: VGR Date: 17/04/2003 06:50:00 English  Type : Comment
sorry, but it should work if theinclude (variables) did not echo things.

is this normal ?
mysql_select_db("$table") or
die ("Could not connect to $db_name. " . mysql_error());

you're connecting to $table and then complaining about $db_name




By: VGR Date: 17/04/2003 06:54:00 English  Type : Comment
you also have a problem here :

mysql_close($db_connect);

whereas you had established :

$dbconnect = mysql_connect("$db_host","$db_user","$db_pass") or
die ("Could not Connect to MySQL. " . mysql_error());


(underscore problem)

you should have received a warning "Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource "
as I did :D

check your error-reporting in php.ini
By: VGR Date: 17/04/2003 07:04:00 English  Type : Comment
this will work :

$login = $_POST['login'];
$pass = $_POST['pass'];
require 'variables.php';

if ($_POST['submit']) {
$dbconnect = mysql_connect($db_host,$db_user,$db_pass) or
die ("Could not Connect to MySQL. " . mysql_error());
mysql_select_db($table) or
die ("Could not connect to $dbtable. " . mysql_error());
$query = "SELECT * FROM main WHERE login = '$login' AND pass = '$pass'";
$result = mysql_query($query) or die("invalid query '$query', error was : ".mysql_error());
$check = mysql_num_rows($result);
mysql_close($dbconnect);
if($check > 0) {
//echo "success!";
session_start();
session_register('login');
header("Location: index.php?".session_name()."=".session_id());
} else {
echo "Error! wrong login or pass!";
}
}


By: PHP newbee Date: 17/04/2003 09:03:00 English  Type : Comment
ok, i fixed it on my own by using those tips about header. but damn, this language to sensative. thanks for your input.
By: VGR Date: 17/04/2003 16:21:00 English  Type : Comment
it's not the language, it's the HTTP protocol 8-)

The Header() procedure can work only if the HTTP response BODY has NOT started, trivially, no ? Because it writes HTTP HEADERS. This has nothing to do with the language stricto sensu.

You have alternatives, though : one being to buffer output using ob_* routines to be able to send HTTP header() commands even if (HTTP BODY) output has started to be buffered, the other one using javascript to relocate at any time during the HTML output
By: sumotimor Date: 21/04/2003 06:18:00 English  Type : Comment
u cant send ne data 2 browser before setting header.

Remove echo "success!"; thing will work fine then.

Do register to be able to answer

EContact
browser fav
page generated in 316.557170 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page