RottenBananas Posted July 17, 2008 Share Posted July 17, 2008 Hey guys, I decided to finally learn php. I asked a friend to gimme a project that would involve a little bit of everything. He told me to make a simple version of myspace. So heres what i'm stumped about. I have my login and everything setup. So a user can setup their profile and view it once they are logged in (using sessions). I want to make it so that anyone can view their page i.e by going to mysite.com/usernameX you would see the usernameX's profile without you having to be logged in. Sorry If I sound vague Im a true newbie. If you guys know how myspace works thats what im looking for. You register, setup your profile, and then anyone can see it by going to myspace.com/username. Thanks for the help! Quote Link to comment Share on other sites More sharing options...
discomatt Posted July 17, 2008 Share Posted July 17, 2008 Well, you'd have to use mod_rewrite ( apache feature ) to get the /username idea going on buy mydomain.com/profile.php?user=myUserName is pretty much what you want to accomplish. profile.php # mysql connction stuff here if ( !isset($_GET['user']) ) exit( 'No profile to display' ); $q = "SELECT `profile`, `info` FROM `users` WHERE `username` = '" . mysql_real_escape_string( $_GET['user'] ) . "'"; $r = mysql_query( $q ); if ( mysql_num_rows($r) < 1 ) exit( 'User not found' ); $profile = mysql_fetch_assoc( $r ); # profile page goes here print_r( $profile ); Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted July 17, 2008 Share Posted July 17, 2008 Ready for the complicated, in depth, difficult code to accomplish this task? It goes like this: you will have a file in your main directory called '.htaccess'(just that, no more no less). Open it in an editor, and add the following lines: RewriteEngine on RewriteRule ^([a-zA-Z0-9_-]+)$ viewprofile.php?user=$1 RewriteRule ^([a-zA-Z0-9_-]+)/$ viewprofile.php?user=$1 Granted, viewprofile.php and the $_GET['user'] variable needs to be replaced with whatever page you have that views the user's profile Quote Link to comment Share on other sites More sharing options...
RottenBananas Posted July 17, 2008 Author Share Posted July 17, 2008 Hey thanks for the reply, I kinda get it but confused on how to code it. So far I have a home.php, profilecp.php and profile.php home is the page shown after the user logs in with links to profile and update profile(profilecp.php) the profilecp.php updates the database with the users entries. so I know how to fetch from the database under this users session ID in profile.php but I guess the whole point is to not use sessions on profile.php since i want the profiles to be public....Im confused Anyone get me? P.S this isnt a live site...just doing it to learn php, its on my localhost...so im assuming i have to create .htaccess myself and stick it in the same directory as the rest of my files? Quote Link to comment Share on other sites More sharing options...
RottenBananas Posted July 18, 2008 Author Share Posted July 18, 2008 nevermind I figured it out using $sql="SELECT * from `users` WHERE `username`='".$_GET['username']."'"; Thanks alot guys! Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted July 18, 2008 Share Posted July 18, 2008 nevermind I figured it out using $sql="SELECT * from `users` WHERE `username`='".$_GET['username']."'"; Thanks alot guys! sorry, had I been to the site earlier, I would've expound on my post by explaining what you figured out. But I feel it's better that you figured it out since we won't be here all the time.. I learned by hours of troubleshooting Quote Link to comment Share on other sites More sharing options...
RottenBananas Posted July 18, 2008 Author Share Posted July 18, 2008 Its no problem, figuring it out is the best way to learn. Having you guys around makes it much easier though! Thanks Again Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.