PHP for multi-user implementation of the replacement Avatar feature sharing

Source: Internet
Author: User
This article mainly introduces PHP for multi-user implementation of the change Avatar function of the relevant data, this article is described in detail, with reference value, the need for friends can refer to the following

A website, in fact, is a combination of a few specific features, and replace the user avatar is in these features. Today to do a test, for different users, to achieve the Avatar upload function.

Let's show you the finished product first:

Ideas

For different users to upload avatars, we want to create a folder for each logged-in user, the name of the folder is the current user's user name prevail.

After the user uploads successfully, jump to the page after the user logs in successfully and refresh the user picture.

Landing page

Form making

<form role= "Form" action= "./forindex.php" ><p class= "Form-group" ><label for= "name" > User name </label ><input type= "text" class= "Form-control" id= "username" name= "username" placeholder= "Please enter name" ></p><p class= "Form-group" ><label for= "inputfile" > File input </label><input type= "password" id= "Inputfile" name = "Password" ><p class= "Help-block" > here is an instance of block-level help text. </p></p><p class= "Form-group" ><label> Please enter the verification code </label><input type= "text" id= " Checkcode "Name=" Checkcode "/> "/><a href=" javascript:void (0); "onclick=" Change () "> Unclear </a></p><script>function Change () {document.getElementById ("Imagecheckcode"). src = "./store.php?r=" + Math.random ();} </script><button type= "Submit" class= "BTN Btn-default" > Submit </button></form>

Verification Code Production

<?phpsession_start ();//must be declared at the beginning of PHP to open session//using GD's Imagecreatetruecolor (); Create a background map $image = Imagecreatetruecolor (100,40);//Generate fill Color $bgcolor = imagecolorallocate ($image, 255,255,255);//fills the fill color onto the background map Imagefill ($ image,0,0, $bgcolor);////////generates a random 4-bit letter and a digital hybrid verification code $checkcode= '; for ($i =0; $i <4; $i + +) {$fontsize = rand (6,8); $ FontColor = Imagecolorallocate ($image, Rand (0,255), Rand (0,255), Rand (0,255));//To prevent the user from being difficult to identify, remove some ambiguous letters and numbers $RAWSTR = ' abcdefghjkmnopqrstuvwxyz23456789 '; $fontcontent = substr ($rawstr, Rand (0,strlen ($RAWSTR)), 1);//splicing of the impending birth of the verification Code $ checkcode.= $fontcontent;//avoid the resulting picture overlap $x + =; $y = rand (10,20); Imagestring ($image, $fontsize, $x, $y, $fontcontent, $ FontColor); }//Save to SESSION variable $_session[' checkcode ']= $checkcode;//Generate some disturbing points, here are 200 for ($i =0; $i <200; $i + +) {$pointcolor = Imagecolorallocate ($image, Rand (50,255), Rand (50,255), Rand (50,255)), Imagesetpixel ($image, Rand (0,100), Rand (0,30), $pointcolor);} Generate a number of interference lines here are 4 for ($i =0; $i <4; $i + +) {//Set to light-colored lines to prevent distracting $linecolor = Imagecolorallocate ($image, rand (50,255), Rand (50,255), Rand (50,255)), Imageline ($image, Rand (0,99), Rand (0,29), Rand (0,99), Rand (0,29), $linecolor);} Header (' content-type:image/png '); Imagepng ($image);//release resources, destroy execution Object Imagedestroy ($image);

JavaScript Refresh Verification Code

<a href= "javascript:void (0);" onclick= "Change ()" > Unclear </a><script>function change () { document.getElementById ("Imagecheckcode"). src = "./store.php?r=" + math.random (); }</script>

Verification page

As the most core of this test is the replacement of the user's avatar, so the user name we do not care, the root of the subject.

Validation logic

<?phpsession_start (); Header ("Content-type:text/html;charset=utf-8"); $username = $_request[' username '];$ Password = $_request[' password '];if (strtolower ($_request[' checkcode ']==$_session[' Checkcode ')) {if (!is_dir ($ username)) {mkdir ($username);} echo "Congratulations on your successful landing!" "." <br/>3 seconds will automatically jump to your homepage! "; $_session[' username '] = $username; Header (" refresh:3;url=./personalpage.php ");} Else{echo "Sorry, the landing failed! "; Header (" refresh:3;url=./index.php ");//echo" <script>window.location.href= "./index.php ' </script>";}

Page Jump

In PHP, there are many ways to implement a page jump first. This article uses the way to add header information, and here are a few small examples of page jumps.

Header function

< PHP//redirect Browser header ("Location:http://blog.csdn.net/marksinoberg"); Ensure that subsequent code is not executed after redirection; >

Note: There can be no spaces between location and:

META tags

<meta http-equiv = "Refresh" content = "1;url=http://blog.csdn.net/marksinoberg" >

Note: Content can be controlled to complete jumps within seconds.

Javascript

< PHP $ url = "Http://bbs.lampbrother.net"; echo "< script language = ' javascript ' type = ' text/javascript ' >"; echo "window.location.href = ' $url '"; echo "</script >";?>

Note: With JavaScript, you can place your code anywhere you want, as long as you are in line with the syntax.

Upload page

Personal homepage

<! DOCTYPE html>

Upload Core

The core of the upload is still a form, we upload the image uploaded to the server, and then PHP using Move_uploaded_file to implement the file migration, upload.

<?phpsession_start (); Header ("Content-type:text/html;charset=utf-8");//The storage location of the attachment, the name of the attachment $path = "./root/"; $ Username = $_session[' username '];//stitching into the name of the file on the server $server_name = $path. $username. ". PNG "; if ($_files[' photo '] [' ERROR ']>0) {die (" Error! ". $_files[' photo ' [' error ']);} if (Move_uploaded_file ($_files[' photo ' [' tmp_name '], $server _name)) {//echo "<BR>". " Upload success! "; echo "Congratulations, upload success!" "." <br/>3 seconds will automatically jump to your homepage! "; Header ("refresh:3;url=./personalpage.php");} Else{//echo "<BR>". " Upload failed! ". $_files[' photo ' [' Error ']; echo "Sorry, uploading the avatar failed!" "; Header (" refresh:2;url=./index.php ");}? >

Final result

Landing page

Validation results

Personal homepage

Latest Avatar

Summary: The above is the entire content of this article, I hope to be able to help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.