0

I have a registration form where the user enters their name, email, username, password, etc. All this information is collected and stored in my database. After the user clicks submit, I want them to be redirected to their profile page. The URL on their profile will look something like this example.php?id=33, but how do I go about doing this?

How do I go about creating the actual dynamic page for them? I know I only have to create one PHP page and a style sheet, and that will apply for all pages, but how do I create a system where right after registration, the user is directed to their profile? I am using PHP and MySQLi. I'd be more than happy if you'd link me to any tutorials or if you could share your knowledge because I've looked everywhere and couldn't find what I was looking for.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
user2896120
  • 3,180
  • 4
  • 40
  • 100
  • 1
    _Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it._ – Chay22 Jun 11 '16 at 08:12
  • @Chay22 Okay forget I said anything about a tutorial. How do I bring about a solution in order for this to work? – user2896120 Jun 11 '16 at 08:15
  • Why don't you use $_SESSION for creating user profile ? It will automatically manage your all users that sign up . And i think it's better than other techniques . – Amir Khan Dec 06 '17 at 14:23

2 Answers2

0

One way of doing the redirect is to use the php header function to send a Location header to the browser:

header('Location: '.$url);

Where $url will be your user profile page. This will only work if you have not sent prior output after managing the registration request.

John Fawcett
  • 249
  • 1
  • 4
  • So let's say id = $id, we'd put header('Location: 'www.example.php?id=$id')? Will this automatically create a new custom page for the user? – user2896120 Jun 11 '16 at 08:20
  • @user2896120 the header won't create a page for you; you should have a typical profile page which is a _template_ and you fill in data by having a dynamic query on the ID value – Matt Jun 11 '16 at 08:27
  • I think I understand what you're saying. So for example, for the main template i'll have an if/else statement getting the id from the url and then checking to see if it's in the database. If it's not in the database, I tell the user the profile does not exist...if it is, i'll display all the necessary information for that existing id. Am I right? – user2896120 Jun 11 '16 at 08:33
  • @user2896120 precisely that yes. – Matt Jun 11 '16 at 09:06
0

First redirect your user to their profile page after they signup:

header('Location: page.php?id='.$ID);

And then on page.php:

$result = mysqli_query($con, "SELECT * FROM users WHERE ID='".$_GET['id']"'");
$row = mysqli_fetch_array($result);
echo $row['name'];
echo $row['email'];

Note that the above code is vulnerable to SQL Injection Attacks which could result in information being altered or deleted from your database.

How can I prevent SQL Injection Attacks? By using PDO with prepared statements:

$stmt = $con->prepare("SELECT * FROM users WHERE ID=:id");
$stmt->bindParam(':id', $_GET['id']);
$stmt->execute();
$row = $stmt->fetch();
echo $row['name'];
echo $row['email');

Make sure to replace $con with your database variable!

Community
  • 1
  • 1
The Codesee
  • 3,714
  • 5
  • 38
  • 78