Jason Paddle
Jason Paddle

Reputation: 1103

Redirect back to previous page with dynamic urls

Sorry for the newbie question but I can't figured it out. When user login I store his data in sessions like this

 $_SESSION['user_id']   = $res['user_id'];
 $_SESSION['username'] = $username;
 $_SESSION['password'] = $password;  
 $_SESSION['level'] = $res['level'];
 $_SESSION['firstname'] = $res['firstname'];
 $_SESSION['lastname']  = $res['lastname'];
 $_SESSION['user_image']= $res['user_image'];
 $_SESSION['email']     = $res['email']; 
 header('Location: users/main.php');

On every page I've put session_start() and everything work just fine. There are some pages like:

http://example.com/users/page.php?user_id=1

When user open page.php he see table filled with data from mysql with buttons edit/delete. When he click on edit button of item 1 is opened page which get if (isset($_GET['item_id'])){...

http://example.com/users/page.php?item_id=1

Now how to redirect user back to page.php?user_id=1 when he click button submit? I've tried with this

header("Location: page.php?user_id=$user_id");

but it doesn't return since properly and URL become http://example.com/users/page.php?user_id=

Upvotes: 0

Views: 124

Answers (1)

Arun Kumar
Arun Kumar

Reputation: 36

$user_id is not defined, so first defined it.

instead of

header("Location: page.php?user_id=$user_id");

Use below code

$user_id=$_SESSION['user_id']l
header("Location: /page.php?user_id=$user_id");

Upvotes: 1

Related Questions