Hey guys, I am having trouble creating a specific redirect that lets users be redirected to a specific page, based on their username. For example, digital point, allows users to be redirected to their own profile after user is authenticated. I want to be able to do that so I can create custom profiles for each member that signs up for my site. Any help would be appriciated! Thanks!
Well, you can create a php redirect using the header() function. Try re-directing with a get variable: <?php if (logged_in($user_id)) { $location = "profile.php?id=" . $user_id; header("location: $location"); } ?>
Thanks for the tip, I will try it out! Where should I replace the variables? like this? <?php if(logged_in(tjw1)) { $location = "profile.php?id=22" . tjw1 header("loaction :$location"); } ?>
Whats tjw1? Is that meant to be a variable (i.e $tjw1) or is it a constant holding some other value? If you stored the users id in some variable just use that in your redirect. As such with posted above, but the method in your post redirects to a fixed id, not a dynamic one from the logged in user. So with GreatMetro's code, if $tjw1 is your variable containing the user id, then use as such: <?php if ( logged_in($tjw1) ) { $location = "profile.php?id=" . $tjw1 header("location: $location"); } ?> PHP: If the page you are redirecting too isn't actually profile.php, then change that accordingly.
Also, logged_in() is a custom function you would write to check if someone is logged in. You could also do it with session variables: session_start(); if (user($user_id)) { $_SESSION['uid'] = $user_id; header("location: profile.php"); } Then on the profile.php, you would also start the session, and check the $_SESSION['uid'] to pull the correct profile information. Using sessions is much more secure than using GET variables.
Using GET it fine in this case, just sanitize the data correctly, and in the case of a profile, type cast it to int and your set. Besides that anyway, GET would need to be used if someone else was viewing the profile.