Forum Moderators: coopster

Message Too Old, No Replies

Redirect to different page

         

tommorrison

4:34 pm on Aug 25, 2008 (gmt 0)

10+ Year Member



Currently i have a database with a table containing username, passwords etc. I can login and logout fine and access protected pages. the only thing is that when a user logs in they all get redirected to the same page. I want them to be redirected to their own seperate page. obviously i will be added users on a regualar basis so is there a way that i can add something to the table in the database and something on the login form so that i wont have to add to it each time i add a new user. I know this is a lot i am asking but i have tried every single way i know possible.

The current code which redirects the user is a follows:

// Register the values & redirect:
$_SESSION = mysqli_fetch_array ($r, MYSQLI_ASSOC);
mysqli_free_result($r);
mysqli_close($dbc);

// Define the URL:
$url = BASE_URL . 'login/memberarea.php'; // Define the URL:ob_end_clean(); // Delete the buffer.
header("Location: $url");
exit(); // Quit the script.

Thanks guys

[edited by: eelixduppy at 7:07 pm (utc) on Aug. 26, 2008]
[edit reason] disabled smileys [/edit]

PHP_Chimp

7:05 pm on Aug 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Seeing as you already have a membersarea.php page and a $_SESSION storing the member why not customize the membersarea page using the information that you have in the $_SESSION?

So you can have:


<h1>Hello <?php echo $_SESSION['name'];?></h1>
<p>Some more customized stuff</p>

That way you dont need to worry about having to add a new page for each new member. It is customized to each member from the information that you hold about them in your database and are storing in your SESSION.

tommorrison

10:28 pm on Aug 26, 2008 (gmt 0)

10+ Year Member



Thanks for that, that seem very useful i will try that out.

Is their a way of creating links due to the different users. i could create another column in the table in the database called 'page' which has the users homepage , for example user14.php how would i then put this on the memberarea.php page?

Thanks very much for your help

Tom

KyleInAction

10:49 pm on Aug 26, 2008 (gmt 0)

10+ Year Member



I'm a bit curious why you want to do it that way. PHP is practically built for dynamic page generation. The idea is that you don't have to create a new .PHP file for every user, but use PHP to dynamically populate one page with each users information.

I.E. One page, memberarea.php, which is passed a user ID, so it looks like this: [someurl.com...] Then you use that ID to go into the database, grab the users info, and populate the page with it. That way you're not creating a bunch of excess files.

Although if you want to do it your way, you could store all of the HTML in the database, and then simply echo it, or save the actual PHP file, and use include to add the user php file to memberarea.php.

tommorrison

11:54 pm on Aug 26, 2008 (gmt 0)

10+ Year Member



how could the user be redirected to e.g [site.com...]

Also i have used the example you gave me and done this.

<h1 class="style29">Welcome <?php echo $_SESSION['first_name'] ;?></h1>
<p class="style29">Please find your pictures on the website. We haven't changed or edited of the pictures. You can share all your pictures with friends buy simply logging onto the website. Please do not share your username and password with others. This is to protect the security of your pictures. </p>
<h1 class="style29">To access your pictures please click the link here <?php echo $_SESSION['page'] ;?></h1>

The only thing is that the second php code will not come up on the page

can you help?

KyleInAction

12:19 am on Aug 27, 2008 (gmt 0)

10+ Year Member



Do a quick var dump of $_SESSION and see if 'page' has a value. If not, then there is a problem somewhere initializing the var.

Quick tip: To save yourself coding time, instead of typing <?php echo $_SESSION['page']; ?> you can simply write <?=$_SESSION['page']?>

tommorrison

12:35 am on Aug 27, 2008 (gmt 0)

10+ Year Member



im sorry i new to php coding so i do not understand what u mean?

Thanks

Tom

[edited by: eelixduppy at 1:39 am (utc) on Aug. 27, 2008]
[edit reason] please keep conversation on the boards, thanks [/edit]

PHP_Chimp

10:40 am on Aug 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you use http://example.com/memberarea.php?user_id=14 as a page it doesnt take a rocket scientist to work out that if they change the url to http://example.com/memberarea.php?user_id=15 they will get another persons page. So unless that info should be public you dont want people to be able to bookmark other people pages. So use a session as this is invisible to the user.

If you want to see what is contained within the $_SESSION array you can use


echo '<pre>';
print_r($_SESSION);
echo '</pre>';

Or

var_dump($_SESSION);

[edited by: dreamcatcher at 6:22 pm (utc) on Aug. 27, 2008]
[edit reason] use example.com. Thanks. [/edit]

KyleInAction

5:34 pm on Aug 27, 2008 (gmt 0)

10+ Year Member



I have a feeling that $_SESSION['page'] isn't be initialized, which is why it's not displaying. Check the code that assigns a value to 'page' and make sure it is functioning properly.

jgrauer

6:17 pm on Aug 27, 2008 (gmt 0)

10+ Year Member



i think an easier way is like this:

members.php?id=123

then, in the members.php page, you can display results based on the id

$id = $_GET['id'];
$user = mysql_fetch_object(mysql_query("SELECT * FROM users WHERE id = '$id'");

if ($id!=$_SESSION['id']){
header("location: bad.php");
}
else
{
your stuff here..
}

now if a user tries to change it, it wont work :)

jgrauer

6:19 pm on Aug 27, 2008 (gmt 0)

10+ Year Member



woops.. forgot to mention to declare $_SESSION['id'] during login!

$userinfo = mysql_fetch_object(mysql_query("SELECT * FROM users where username = '$username' and password = '$password'"));

$_SESSION['id'] = $userinfo->id;

and to correct a code up top

$user = mysql_fetch_object(mysql_query("SELECT * FROM users WHERE id = '$id'");

with

$user = mysql_fetch_object(mysql_query("SELECT * FROM users WHERE id = '$id'"));