Welcome to Dream.In.Code
Become a PHP Expert!

Join 150,139 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,276 people online right now. Registration is fast and FREE... Join Now!




Redirecting

2 Pages V  1 2 >  
Reply to this topicStart new topic

Redirecting, How to use header?

Sayid Ahmed
23 Aug, 2008 - 05:30 AM
Post #1

New D.I.C Head
*

Joined: 20 Aug, 2008
Posts: 29


My Contributions
Hello,

I thought I'd start a new thread since my last one got a bit hectic. I've made a registration form where people sign up with username and password. The code I've used is:

CODE
$username = $_POST['username'];
$password = $_POST['password'];

$sql = "INSERT INTO users (username, password) VALUES ('".$username."', '".md5($password)."')";

mysql_query($sql)or die(mysql_error());


And the code for the form I've used is:

CODE

<form action="saveuser.php" method="post" >
<table>
<tr>
    <td>Username</td>
    <td><input type="text" name="username" id="uname_txt" /></td>
</tr>
<tr>
    <td>Password</td>
    <td><input type="password" name="password" id="pwd_txt" /></td>
</tr>
<tr>
    <td></td>
    <td><input type="submit" id="sub_btn" name="sub_btn" value="Submit" /></td>
</tr>
</table>
</form>


What i want to know is, if they've inputted an incorrect username or password (like too many characters or the username is taken), how can i redirect the user to a different page. I assume i have to use the header() function but I'm not sure how.

Thank you

This post has been edited by Sayid Ahmed: 23 Aug, 2008 - 05:33 AM
User is offlineProfile CardPM
+Quote Post

MitkOK
RE: Redirecting
23 Aug, 2008 - 05:44 AM
Post #2

D.I.C Regular
Group Icon

Joined: 9 Aug, 2007
Posts: 326



Thanked: 12 times
Dream Kudos: 250
My Contributions
You have to write simple if construction to check inputted data and redirect if needed.
User is offlineProfile CardPM
+Quote Post

chili5
RE: Redirecting
23 Aug, 2008 - 05:48 AM
Post #3

D.I.C Addict
****

Joined: 28 Dec, 2007
Posts: 763



Thanked: 4 times
My Contributions
Exactly, MitkOK is right.

php

if ($_POST['username'] != $row['username'] or md5($_POST['password']) != $row['password']) {
header("Location: login.php");
} else {
echo "$_POST[username] you are now logged in.";
}


If the user name doesn't match the username in the database or the password hashes are incorrect, then you can redirect the user. Just use an if-else block like above.
User is offlineProfile CardPM
+Quote Post

Sayid Ahmed
RE: Redirecting
23 Aug, 2008 - 06:16 AM
Post #4

New D.I.C Head
*

Joined: 20 Aug, 2008
Posts: 29


My Contributions
This is a signup page, not a log in. But based on what you said, if the usernames do match, then they get redirected by this code:

CODE

    if ($_POST['username'] = $row['username']) {  
    header("Location: unsuccessful.php");  
    } else {  
    header("Location: successful.php");
    }  


Am i correct?
User is offlineProfile CardPM
+Quote Post

chili5
RE: Redirecting
23 Aug, 2008 - 06:34 AM
Post #5

D.I.C Addict
****

Joined: 28 Dec, 2007
Posts: 763



Thanked: 4 times
My Contributions
Oops, yeah that will work. biggrin.gif


User is offlineProfile CardPM
+Quote Post

Sayid Ahmed
RE: Redirecting
23 Aug, 2008 - 07:56 AM
Post #6

New D.I.C Head
*

Joined: 20 Aug, 2008
Posts: 29


My Contributions
It doesn't seem to work, it will still make a new entry to the database.

BTW, what is the variable $row for?
User is offlineProfile CardPM
+Quote Post

JBrace1990
RE: Redirecting
23 Aug, 2008 - 11:05 AM
Post #7

D.I.C Regular
Group Icon

Joined: 9 Mar, 2008
Posts: 479



Thanked: 24 times
Dream Kudos: 350
My Contributions
$row is what you pull from the database...

you need to query the database to pull the persons username... if they username exists, then count($row['username']) will equal 1... IF it equals one, then redirect to unsuccessful.php... if it equals 0, then redirect to successfuly.php...

if you don't understand it, just post here and i'll write up the code... but please TRY it first =)
User is offlineProfile CardPM
+Quote Post

Sayid Ahmed
RE: Redirecting
23 Aug, 2008 - 01:56 PM
Post #8

New D.I.C Head
*

Joined: 20 Aug, 2008
Posts: 29


My Contributions
OK i understand what you mean, but sadly it didn't work when i tried it, i started off with this:

CODE

    $count = count($row['username']);
    
            if ($count > 0) {
         header("Location: unsuccessful.php");
    } else {
         header("Location: successful.php");
    }


I messed around with it but still no result. The header part does work tho, cos i made $count = 1 and redirected to unsuccessful.php

This post has been edited by Sayid Ahmed: 23 Aug, 2008 - 02:11 PM
User is offlineProfile CardPM
+Quote Post

JBrace1990
RE: Redirecting
23 Aug, 2008 - 03:42 PM
Post #9

D.I.C Regular
Group Icon

Joined: 9 Mar, 2008
Posts: 479



Thanked: 24 times
Dream Kudos: 350
My Contributions
you should use $count = count($row)... you want to use the entire array
User is offlineProfile CardPM
+Quote Post

Sayid Ahmed
RE: Redirecting
24 Aug, 2008 - 08:04 AM
Post #10

New D.I.C Head
*

Joined: 20 Aug, 2008
Posts: 29


My Contributions
QUOTE(JBrace1990 @ 23 Aug, 2008 - 04:42 PM) *

you should use $count = count($row)... you want to use the entire array


I'm sorry, you've lost me now blink.gif
User is offlineProfile CardPM
+Quote Post

JBrace1990
RE: Redirecting
24 Aug, 2008 - 11:03 AM
Post #11

D.I.C Regular
Group Icon

Joined: 9 Mar, 2008
Posts: 479



Thanked: 24 times
Dream Kudos: 350
My Contributions
php
<?php
$sql = mysql_query("SELECT username FROM users WHERE username = '$username'")or die(mysql_error());
$row = mysql_fetch_array($sql);
$count = count($row);

if($count > 1){
header("Location: unsuccessful.php");
}else{
header("Location: successful.php");
}
?>

User is offlineProfile CardPM
+Quote Post

Sayid Ahmed
RE: Redirecting
24 Aug, 2008 - 12:50 PM
Post #12

New D.I.C Head
*

Joined: 20 Aug, 2008
Posts: 29


My Contributions
Wehay, cheers for that, i guess i better go read a mysql tutorial as well as PHP then.

here's the complete code I used:

php

<?php

$username = $_POST['username'];
$password = $_POST['password'];

$match = mysql_query("SELECT username FROM users WHERE username = '$username'")or die(mysql_error());
$row = mysql_fetch_array($match);
$count = count($row);

if($count > 1){
header("Location: unsuccessful.php");
}else{
header("Location: successful.php");
$sql = "INSERT INTO users (username, password) VALUES ('".$username."', '".md5($password)."')";
mysql_query($sql)or die(mysql_error());
}

?>


This post has been edited by Sayid Ahmed: 24 Aug, 2008 - 12:51 PM
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:11AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month