1

I have created a registration & login system, it is working properly. But, I only knew how to make a register & login system, I don't know how to make a link for every registered user

like : example.com/marwan
example.com/user/marwan

Here is my Register PHP Code :

<?php
error_reporting(0);
session_start();

if( isset($_SESSION['user_id']) ){
 header("Location: /");
}

require 'includes/database.php';

$message = '';

if(!empty($_POST['email']) && !empty($_POST['password'])):
 
 // Enter the new user in the database
 $sql = "INSERT INTO users (full_name, email, password, phone, country) VALUES (:full_name, :email, :password, :phone, :country)";
 $stmt = $conn->prepare($sql);

 $stmt->bindParam(':full_name', $_POST['full_name']);
 $stmt->bindParam(':email', $_POST['email']);
 $stmt->bindParam(':password', password_hash($_POST['password'], PASSWORD_BCRYPT));
 $stmt->bindParam(':phone', $_POST['phone']);
 $stmt->bindParam(':country', $_POST['country']);

 if( $stmt->execute() ):
   header("Location: account-created.php");
 else:
  header("Location: failed.php");
 endif;

endif;

?>

And my Login code is :

<?php

session_start();

if( isset($_SESSION['user_id']) ){
 header("Location: /tregolapp/home");
}

require 'includes/database.php';

if(!empty($_POST['email']) && !empty($_POST['password'])):
 
 $records = $conn->prepare('SELECT id,email,password FROM users WHERE email = :email');
 $records->bindParam(':email', $_POST['email']);
 $records->execute();
 $results = $records->fetch(PDO::FETCH_ASSOC);

 $message = '';

 if(count($results) > 0 && password_verify($_POST['password'], $results['password']) ){

  $_SESSION['user_id'] = $results['id'];
  header("Location: /tregolapp/index.php");

 } else {
  header("Location: /tregolapp/failed");
 }

endif;

?>

Can someone help me please?

  • With something like that you might be best having `example.com/user/marwan` that way you could use your `.htaccess` to read `/marwan` and use the path `/?user=marwan` allowing a dynami page to run for that user. Many different ways of doing this so it's more down to what you want. – NewToJS Jul 07 '17 at 01:18
  • Hello NewToJS, Can you give me a code or something to add so it makes the system I am asking for or help me by giving a guide *to do* so it helps me doing that. – Marwan K. Elzeer Jul 07 '17 at 01:20
  • Well if you want to use your `.htaccess` then you can look at this post https://stackoverflow.com/questions/16388959/url-rewriting-with-php but as I have said, this is just one of many ways of doing it... If you think of a specific way and make an attempt of your own but have problems I'm sure someone can help you but at this moment your question doesn't really indicate a specific way/attempt. It's like you're for a tutorial rather than a solution. – NewToJS Jul 07 '17 at 01:27

1 Answers1

0

What is your purpose in giving each user their "own URL"?

If you want each user to see their "own information" upon login, then after login go to a common "home page" while holding a session id. Use that id to pull "user specific" data from your database and display it on the "home page".

dankinship
  • 19
  • 1
  • 8
  • Hello, I have already done the "my profile page" but, I want to make a system where users can view other users account, because I am using it for a social network, I need people to be able to vies other accounts. – Marwan K. Elzeer Jul 07 '17 at 16:15
  • So, use session to hold your user's id (i.e the fact that they are logged in), then pull any information you want from the database. Why would you want to just let them navigate to a static page? All of your pages (after login) can be dynamic with data coming from the database. To make things simple, in php you can have a particular php "page" be (e.g.) a "view_others_data.php" page. That page will allow a logged in user to view other people's accounts, and you can use a "permissions" type of table to determine what kind of information the "logged in user" is allowed to view on that page. – dankinship Jul 09 '17 at 02:38