Jump to content

Recommended Posts

I want to make a script redirect the user back to the Index.php page if it found that the login was correct. What is the best way to do this?

Thanks.


[pre]if(mysql_affected_rows()==0){
$_SESSION['in'] = 0;
            //BACK TO LOGIN.PHP
}
else {
$_SESSION['in'} = 1;
//REDIRECT TO INDEX.PHP HERE
}[/pre]
Link to comment
https://forums.phpfreaks.com/topic/33916-another-noobie-question/
Share on other sites

[url=http://www.php.net/header]header[/url]

as long as nothing has been output to the browser previously, then this should work:

[code]
<?php
header("Location: /login.php");
exit;
?>
[/code]

using header alone like this does not IMMEDIATELY redirect, hence my 'exit' the line after.

cheers
I've already sent something to the browser I think.

The header isn't working. Its giving me the usual "headers already sent."

Here is the whole form

[code]<? session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>CheckLogin</title>
</head>

<body>

<?php
@mysql_connect("localhost","allan","ah1018$") or die("Cannot Connect to DB!");
@mysql_select_db("trademymedia")or die("Cannot select DB");

$Email = $_POST["uEmail"];
$Password = $_POST["uPassword"];

$r = mysql_query("SELECT * FROM customerinfo WHERE Email = '".$Email."' AND Password = '".$Password."'");

if(!$r) {
$err=mysql_error();
print $err;
exit();}

if(mysql_affected_rows()==0){
$_SESSION['in'] = 0;
}
else {
$_SESSION['in'] = 1;
header("index.php");
}

?>
</body>
</html>[/code]
move all your php code up to the top, above the HTML. i cant see any specific reason for it to be there, so you should be fine.

and you need header("Location: index.php"), not header("index.php"), followed by an 'exit'

[code]
<?php
session_start();

@mysql_connect("localhost","allan","ah1018$") or die("Cannot Connect to DB!");
@mysql_select_db("trademymedia")or die("Cannot select DB");

$Email = $_POST["uEmail"];
$Password = $_POST["uPassword"];

$r = mysql_query("SELECT * FROM customerinfo WHERE Email = '".$Email."' AND Password = '".$Password."'");

if(!$r) {
$err=mysql_error();
print $err;
exit();}

if(mysql_affected_rows()==0){
$_SESSION['in'] = 0;
}
else {
$_SESSION['in'] = 1;
header("Location: index.php");
exit;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>CheckLogin</title>
</head>

<body>


</body>
</html>[/code]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.